C# Basic

C# 编程基础
变量声明
1None.gifint x;
2None.gifString s;
3None.gifString s1, s2;
4None.gifObject o;
5None.gifObject obj = new Object();
6None.gifpublic String name;

语句:

1None.gifResponse.Write("foo");

注释
1None.gif// This is a comment
2None.gif
3ExpandedBlockStart.gifContractedBlock.gif/**//*
4InBlock.gifThis
5InBlock.gifis
6InBlock.gifa
7InBlock.gifmultiline
8InBlock.gifcomment
9ExpandedBlockEnd.gif*/

访问索引属性:
1None.gifString s = Request.QueryString["Name"];
2None.gifString value = Request.Cookies["key"];

声明索引属性
1None.gif// Default Indexed Property
2ExpandedBlockStart.gifContractedBlock.gifpublic String this[String name] dot.gif{
3ExpandedSubBlockStart.gifContractedSubBlock.gif    get dot.gif{
4InBlock.gif        return (String) lookuptable[name];
5ExpandedSubBlockEnd.gif    }

6ExpandedBlockEnd.gif}

声明简单属性
 1ExpandedBlockStart.gifContractedBlock.gifpublic String name dot.gif{
 2ExpandedSubBlockStart.gifContractedSubBlock.gif  get dot.gif{
 3InBlock.gif    dot.gif
 4InBlock.gif    return dot.gif;
 5ExpandedSubBlockEnd.gif  }

 6InBlock.gif
 7ExpandedSubBlockStart.gifContractedSubBlock.gif  set dot.gif{
 8InBlock.gif    dot.gif = value;
 9ExpandedSubBlockEnd.gif  }

10ExpandedBlockEnd.gif}

声明和使用枚举
 1None.gif// Declare the Enumeration
 2ExpandedBlockStart.gifContractedBlock.gifpublic enum MessageSize dot.gif{
 3InBlock.gif
 4InBlock.gif    Small = 0,
 5InBlock.gif    Medium = 1,
 6InBlock.gif    Large = 2
 7ExpandedBlockEnd.gif}

 8None.gif
 9None.gif// Create a Field or Property
10None.gifpublic MessageSize msgsize;
11None.gif
12None.gif// Assign to the property using the Enumeration values
13None.gifmsgsize = Small;

枚举集合
1ExpandedBlockStart.gifContractedBlock.gifforeach ( String s in coll ) dot.gif{
2InBlock.gif dot.gif
3ExpandedBlockEnd.gif}

声明和使用方法
 1None.gif// Declare a void return function
 2ExpandedBlockStart.gifContractedBlock.gifvoid voidfunction() dot.gif{
 3InBlock.gif dot.gif
 4ExpandedBlockEnd.gif}

 5None.gif
 6None.gif// Declare a function that returns a value
 7ExpandedBlockStart.gifContractedBlock.gifString stringfunction() dot.gif{
 8InBlock.gif dot.gif
 9InBlock.gif    return (String) val;
10ExpandedBlockEnd.gif}

11None.gif
12None.gif// Declare a function that takes and returns values
13ExpandedBlockStart.gifContractedBlock.gifString parmfunction(String a, String b) dot.gif{
14InBlock.gif dot.gif
15InBlock.gif    return (String) (a + b);
16ExpandedBlockEnd.gif}

17None.gif
18None.gif// Use the Functions
19None.gifvoidfunction();
20None.gifString s1 = stringfunction();
21None.gifString s2 = parmfunction("Hello""World!");

自定义属性
1None.gif// Stand-alone attribute
2None.gif[STAThread]
3None.gif
4None.gif// Attribute with parameters
5None.gif[DllImport("ADVAPI32.DLL")]
6None.gif
7None.gif// Attribute with named parameters
8None.gif[DllImport("KERNEL32.DLL",CharSet=CharSet.Auto)]

数组
1None.gifString[] a = new String[3];
2None.gifa[0= "1";
3None.gifa[1= "2";
4None.gifa[2= "3";
5None.gif
6None.gifString[][] a = new String[3][3];
7None.gifa[0][0= "1";
8None.gifa[1][0= "2";
9None.gifa[2][0= "3";

初始化
1None.gifString s = "Hello World";
2None.gifint i = 1;
3ExpandedBlockStart.gifContractedBlock.gifdouble[] a =  dot.gif3.004.005.00 };

If 语句
1ExpandedBlockStart.gifContractedBlock.gifif (Request.QueryString != nulldot.gif{
2InBlock.gif  dot.gif
3ExpandedBlockEnd.gif}

Case 语句
 1ExpandedBlockStart.gifContractedBlock.gifswitch (FirstName) dot.gif{
 2InBlock.gif  case "John" :
 3InBlock.gif    dot.gif
 4InBlock.gif    break;
 5InBlock.gif  case "Paul" :
 6InBlock.gif    dot.gif
 7InBlock.gif    break;
 8InBlock.gif  case "Ringo" :
 9InBlock.gif    dot.gif
10InBlock.gif    break;
11InBlock.gif  default:
12InBlock.gif    dot.gif
13InBlock.gif    break;
14ExpandedBlockEnd.gif}

For 循环
1None.giffor (int i=0; i<3; i++)
2None.gif  a(i) = "test";

While 循环
1None.gifint i = 0;
2ExpandedBlockStart.gifContractedBlock.gifwhile (i<3dot.gif{
3InBlock.gif  Console.WriteLine(i.ToString());
4InBlock.gif  i += 1;
5ExpandedBlockEnd.gif}

异常处理
1ExpandedBlockStart.gifContractedBlock.giftry dot.gif{
2InBlock.gif    // Code that throws exceptions
3ExpandedBlockStart.gifContractedBlock.gif}
 catch(OverflowException e) dot.gif{
4InBlock.gif    // Catch a specific exception
5ExpandedBlockStart.gifContractedBlock.gif}
 catch(Exception e) dot.gif{
6InBlock.gif    // Catch the generic exceptions
7ExpandedBlockStart.gifContractedBlock.gif}
 finally dot.gif{
8InBlock.gif    // Execute some cleanup code
9ExpandedBlockEnd.gif}

字符串连接
 1None.gif// Using Strings
 2None.gifString s1;
 3None.gifString s2 = "hello";
 4None.gifs2 += " world";
 5None.gifs1 = s2 + " !!!";
 6None.gif
 7None.gif// Using StringBuilder class for performance
 8None.gifStringBuilder s3 = new StringBuilder();
 9None.gifs3.Append("hello");
10None.gifs3.Append(" world");
11None.gifs3.Append(" !!!");

事件处理程序委托
1None.gifvoid MyButton_Click(Object sender,
2ExpandedBlockStart.gifContractedBlock.gif                    EventArgs E) dot.gif{
3InBlock.gifdot.gif
4ExpandedBlockEnd.gif}

声明事件:
1None.gif// Create a public event
2None.gifpublic event EventHandler MyEvent;
3None.gif
4None.gif// Create a method for firing the event
5ExpandedBlockStart.gifContractedBlock.gifprotected void OnMyEvent(EventArgs e) dot.gif{
6InBlock.gif      MyEvent(this, e);
7ExpandedBlockEnd.gif}

向事件添加事件处理程序或从事件移除事件处理程序:
1None.gifControl.Change += new EventHandler(this.ChangeEventHandler);
2None.gifControl.Change -= new EventHandler(this.ChangeEventHandler);

强制类型转换:
1None.gifMyObject obj = (MyObject)Session["Some Value"];
2None.gifIMyObject iObj = obj;

转换:
1None.gifint i = 3;
2None.gifString s = i.ToString();
3None.gifdouble d = Double.Parse(s);

带继承的类定义:
 1None.gifusing System;
 2None.gif
 3ExpandedBlockStart.gifContractedBlock.gifnamespace MySpace dot.gif{
 4InBlock.gif
 5ExpandedSubBlockStart.gifContractedSubBlock.gif  public class Foo : Bar dot.gif{
 6InBlock.gif
 7InBlock.gif    int x;
 8InBlock.gif
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    public Foo() dot.gif{ x = 4; }
10ExpandedSubBlockStart.gifContractedSubBlock.gif    public void Add(int x) dot.gifthis.x += x; }
11ExpandedSubBlockStart.gifContractedSubBlock.gif    override public int GetNum() dot.gifreturn x; }
12ExpandedSubBlockEnd.gif  }

13InBlock.gif
14ExpandedBlockEnd.gif}

15None.gif
16None.gif// csc /out:librarycs.dll /t:library
17None.gif// library.cs

实现接口:
1ExpandedBlockStart.gifContractedBlock.gifpublic class MyClass : IEnumerable dot.gif{
2InBlock.gif dot.gif
3InBlock.gif
4ExpandedSubBlockStart.gifContractedSubBlock.gif    IEnumerator IEnumerable.GetEnumerator() dot.gif{
5InBlock.gif         dot.gif
6ExpandedSubBlockEnd.gif    }

7ExpandedBlockEnd.gif}

带 Main 方法的类定义:
 1None.gifusing System;
 2None.gif
 3ExpandedBlockStart.gifContractedBlock.gifpublic class ConsoleCS dot.gif{
 4InBlock.gif
 5ExpandedSubBlockStart.gifContractedSubBlock.gif  public ConsoleCS() dot.gif{
 6InBlock.gif    Console.WriteLine("Object Created");
 7ExpandedSubBlockEnd.gif  }

 8InBlock.gif
 9ExpandedSubBlockStart.gifContractedSubBlock.gif  public static void Main (String[] args) dot.gif{
10InBlock.gif    Console.WriteLine("Hello World");
11InBlock.gif    ConsoleCS ccs = new ConsoleCS();
12ExpandedSubBlockEnd.gif  }

13InBlock.gif
14ExpandedBlockEnd.gif}

15None.gif
16None.gif// csc /out:consolecs.exe /t:exe console.cs

标准模块:
 1None.gifusing System;
 2None.gif
 3ExpandedBlockStart.gifContractedBlock.gifpublic class Module dot.gif{
 4InBlock.gif
 5ExpandedSubBlockStart.gifContractedSubBlock.gifpublic static void Main (String[] args) dot.gif{
 6InBlock.gif  Console.WriteLine("Hello World");
 7ExpandedSubBlockEnd.gif}

 8InBlock.gif
 9ExpandedBlockEnd.gif}

10None.gif// csc /out:consolecs.exe /t:exe console.cs

转载于:https://www.cnblogs.com/shinyzhu/archive/2005/06/04/167898.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值