Visual C# 通用语言示例主题(一)(MSDN整理)

这篇博客介绍了几个Visual C#编程实例,包括使用break语句跳出循环,调用Process.Start打开网页,查找程序集资源,生成多行字符串以及初始化数组的方法。通过这些示例,读者可以更深入地理解C#的基本语法和操作。
1.代码:跳出循环语句 (Visual C#)

本示例使用 break 语句在整数计数器达到 10 时跳出 for 循环。

示例:

for (int counter = 1; counter <= 1000; counter++)
{
    if (counter == 10)
       break;
    Console.WriteLine(counter);
}

编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。

2.代码:调用对象上的方法 (Visual C#)

本示例调用 Console 类上的 WriteLine 方法。
示例:
Console.WriteLine("This line will appear in the console window");
编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
3.代码:捕捉异常 (Visual C#)
本示例使用 try- catch 块将被零除的情况作为异常进行捕捉。
示例:
int top = 0, bottom = 0, result = 0;
try
{
    result = top / bottom;
}
catch (System.DivideByZeroException ex)
{
    Console.WriteLine("{0} Exception caught.", ex);
}
编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
4.代码:声明属性 (Visual C#)
本示例将声明一个实例属性。
示例:
private string name;
// A read-write instance property:
public string NameProperty
{
   get
   {
      return name;
   }
   set
   {
      name = value;
   }
}
编译代码-----》代码必须显示在类或结构内。
可靠编程-----》可以使用 get 访问器返回字段值或者计算该值后返回它,例如:
get
{
    return (name != null) ? name : "NA";
}
        -----》请不要使用 get 访问器更改对象的状态,例如:
get
{
    return myNumericField++;
}
5.代码:声明数组 (Visual C#)
本示例显示用三种不同的方法声明不同类型的数组:一维、多维和交错。
示例:
// Single-dimensional arrays.
int[] myArray1 = new int [5];
string[] myArray2 = new string[6];
// Multidimensional arrays.
int[,] myArray3 = new int[4,2];
int[,,] myArray4 = new int [4,2,3];
// Jagged array.
int[][] myArray5 = new int[3][];
编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
可靠编程-----》请确保在使用交错数组之前已经初始化过其元素,如下所示:
myArray5[0] = new int[7];
myArray5[1] = new int[5];
myArray5[2] = new int[2];
6.代码:确定两个日期之间的间隔 (Visual C#)
本示例计算两个日期之间相差的天数并为该差额构造一个 TimeSpan 值。
示例:
DateTime oldDate = new DateTime(2002,7,15);
DateTime newDate = DateTime.Now;
// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;
// Difference in days.
int differenceInDays = ts.Days;
Console.WriteLine("Difference in days: {0} ", differenceInDays);
编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
7.代码:在 Internet Explorer 中显示 Web 页 (Visual C#)

本示例使用 Process.Start 方法在 Internet Explorer 中显示 www.microsoft.com

示例:

System.Diagnostics.Process.Start(
"C://Program Files//Internet Explorer//IExplore.exe",
www.microsoft.com);

编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。

        -----》如果程序安装在其他位置,请将 Start 的第一个参数替换为 IExplore.exe 的路径。

8.代码:查找程序集中的资源名称 (Visual C#)

本示例创建一个字符串,该字符串包含程序集中所有资源的名称。

示例:

System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
string [] resources = thisExe.GetManifestResourceNames();
string list = "";
// Build the string of resources.
foreach (string resource in resources)
   list += resource + "/r/n";

编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。

9.代码:生成多行字符串 (Visual C#)

本示例说明两种构造多行字符串的方法。
示例:
string myString1 = "This is the first line of my string./n" +
                   "This is the second line of my string./n" +
                   "This is the third line of the string./n";
string myString2 = @"This is the first line of my string.
This is the second line of my string.
This is the third line of the string."
编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
10.代码:从类进行继承 (Visual C#)
本示例定义 Circle 类和 Rectangle 类(它们是从 Shape 类继承的)以及 Square 类(它是从 Rectangle 类继承的)。
示例:
public class Shape
{
    // Definitions of properties, methods, fields, and events.
}
public class Circle : Shape
{
    // Specialized properties, methods, fields, events for Circle.
}
public class Rectangle : Shape
{
    // Specialized properties, methods, fields, events for Rectangle.
}
public class Square : Rectangle
{
    // Specialized properties, methods, fields, events for Square.
}
编译代码-----》启动新的控制台应用程序。

        ------》复制代码,并将其粘贴至紧挨 Class1 声明的前面或后面。

11.代码:初始化数组 (Visual C#)

本示例显示用三种不同方法初始化不同类型的数组:一维、多维和交错。
示例:
// Single-dimensional array (numbers).
int[] n1 = new int[4] {2, 4, 6, 8};
int[] n2 = new int[] {2, 4, 6, 8};
int[] n3 = {2, 4, 6, 8};
// Single-dimensional array (strings).
string[] s1 = new string[3] {"John", "Paul", "Mary"};
string[] s2 = new string[] {"John", "Paul", "Mary"};
string[] s3 = {"John", "Paul", "Mary"};
// Multidimensional array.
int[,] n4 = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
int[,] n5 = new int[,] { {1, 2}, {3, 4}, {5, 6} };
int[,] n6 = { {1, 2}, {3, 4}, {5, 6} };
// Jagged array.
int[][] n7 = new int[2][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} };
int[][] n8 = new int[][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} };
int[][] n9 = { new int[] {2,4,6}, new int[] {1,3,5,7,9} };
编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
12.代码:循环访问集合 (Visual C#)
本示例使用 Hashtable 类上的 Add 方法向 Hashtable 集合添加项。然后使用 foreach 语句循环访问该集合。
示例:
Hashtable phones = new Hashtable();
// Add items.
phones.Add("John", "123-4567");
phones.Add("Enju", "351-8765");
phones.Add("Molly", "221-5678");
phones.Add("James", "010-4077");
phones.Add("Nelly", "110-5699");
phones.Add("Leah", "922-5699");
// Iterate through the collection.
Console.WriteLine("Name/t/tNumber");
foreach (string name in phones.Keys)
{
    Console.WriteLine(name +"/t"+ phones[name]);
}
编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。

        -----》将 using 指令添加至 System.Collections 命名空间。

13.代码:循环访问数组 (Visual C#)
本示例使用 foreach 语句访问和显示数组项。
示例:
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
foreach (int element in numbers)
{
    System.Console.WriteLine(element);
}
编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
14.代码:将 Object 数组传递给方法 (Visual C#)

本示例显示如何将对象数组传递给 DisplayMyCollection 方法,这种方法使用 params 关键字接受任何数目的参数。

示例:

class MyBoxingClass
{
   public static void DisplayMyCollection(params object[] anArray)
   {
      foreach (object obj in anArray)
      {
         System.Console.Write(obj + "/t");
      }
      // Suspend the screen.
      System.Console.ReadLine();
   } 
   static void Main()
   {
      DisplayMyCollection(101, "Visual C# Basics", 2002);
   }
}

编译代码------》可以直接使用命令行编译该示例,也可以使用 Visual Studio IDE 将代码粘贴到控制台应用程序中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值