本示例使用 break 语句在整数计数器达到 10 时跳出 for 循环。
示例:
for (int counter = 1; counter <= 1000; counter++)
{
if (counter == 10)
break;
Console.WriteLine(counter);
}
编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
2.代码:调用对象上的方法 (Visual C#)
try
{
result = top / bottom;
}
catch (System.DivideByZeroException ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
// A read-write instance property:
public string NameProperty
{
get
{
return name;
}
set
{
name = value;
}
}
{
return (name != null) ? name : "NA";
}
{
return myNumericField++;
}
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][];
myArray5[1] = new int[5];
myArray5[2] = new int[2];
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);
本示例使用 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#)
"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."
{
// 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#)
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} };
// 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]);
}
-----》将 using 指令添加至 System.Collections 命名空间。
foreach (int element in numbers)
{
System.Console.WriteLine(element);
}
本示例显示如何将对象数组传递给 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 将代码粘贴到控制台应用程序中。