标签:搜索 number ide www 声明 names val try 常用
例如:using System; 一般都会出现在*.cs中。
2.using别名。using + 别名 = 包括详细命名空间信息的具体的类型。|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
using System;using aClass = NameSpace1.MyClass;using bClass = NameSpace2.MyClass;namespace NameSpace1 { public class MyClass { public override string ToString() { return "You are in NameSpace1.MyClass"; } }}namespace NameSpace2 { class MyClass { public override string ToString() { return "You are in NameSpace2.MyClass"; } }}namespace testUsing{ using NameSpace1; using NameSpace2; /// <summary> /// Class1 的摘要说明。 /// </summary> class Class1 { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { // // TODO: 在此处添加代码以启动应用程序 // aClass my1 = new aClass(); Console.WriteLine(my1); bClass my2 = new bClass(); Console.WriteLine(my2); Console.WriteLine("Press any key"); Console.Read(); } }} |
3.using语句,定义一个范围,在范围结束时处理对象。
场景:
当在某个代码段中使用了类的实例,而希望无论因为什么原因,只要离开了这个代码段就自动调用这个类实例的Dispose。
要达到这样的目的,用try...catch来捕捉异常也是可以的,但用using也很方便。
|
1
2
3
4
|
using (Class1 cls1 = new Class1(), cls2 = new Class1()){ // the code using cls1, cls2} // call the Dispose on cls1 and cls2 |
Array.Sort(nums/*数组名称*/);
Array.Reverse(nums/*数组名称*/);
public static void UseIndexOf()
{
string str = "hello cat. hi boatlet. How are you cat? Fine, Thanks!";
//搜索字符串,初始位置
int index = str.IndexOf("cat", 0);
int i = 1;
if(index != -1)
{
Console.WriteLine("cat 第{0}次出现位置为{1}", i, index);
while(index != -1)
{
index = str.IndexOf("cat", index + 1);
i++;
if(index == -1)
{
break;
}
Console.WriteLine("cat 第{0}次出现位置为{1}", i, index);
}
}
标签:搜索 number ide www 声明 names val try 常用
原文地址:http://www.cnblogs.com/my-cat/p/7458125.html