C# Basic
变量声明:
注释 :
访问索引属性:
声明索引属性:
声明简单属性:
声明和使用枚举:
枚举集合:
声明和使用方法:
自定义属性:
数组:
初始化:
If 语句:
Case 语句:
For 循环:
While 循环:
异常处理:
字符串连接:
事件处理程序委托:
声明事件:
向事件添加事件处理程序或从事件移除事件处理程序:
强制类型转换:
转换:
带继承的类定义:
实现接口:
带 Main 方法的类定义:
标准模块:
1int x;
2String s;
3String s1, s2;
4Object o;
5Object obj = new Object();
6public String name;
2String s;
3String s1, s2;
4Object o;
5Object obj = new Object();
6public String name;
语句:
1Response.Write("foo");
注释 :
1// This is a comment
2
3/*
4This
5is
6a
7multiline
8comment
9*/
2
3/*
4This
5is
6a
7multiline
8comment
9*/
访问索引属性:
1String s = Request.QueryString["Name"];
2String value = Request.Cookies["key"];
2String value = Request.Cookies["key"];
声明索引属性:
1// Default Indexed Property
2public String this[String name] {
3 get {
4 return (String) lookuptable[name];
5 }
6}
2public String this[String name] {
3 get {
4 return (String) lookuptable[name];
5 }
6}
声明简单属性:
1public String name {
2 get {
3
4 return ;
5 }
6
7 set {
8 = value;
9 }
10}
2 get {
3
4 return ;
5 }
6
7 set {
8 = value;
9 }
10}
声明和使用枚举:
1// Declare the Enumeration
2public enum MessageSize {
3
4 Small = 0,
5 Medium = 1,
6 Large = 2
7}
8
9// Create a Field or Property
10public MessageSize msgsize;
11
12// Assign to the property using the Enumeration values
13msgsize = Small;
2public enum MessageSize {
3
4 Small = 0,
5 Medium = 1,
6 Large = 2
7}
8
9// Create a Field or Property
10public MessageSize msgsize;
11
12// Assign to the property using the Enumeration values
13msgsize = Small;
枚举集合:
1foreach ( String s in coll ) {
2
3}
2
3}
声明和使用方法:
1// Declare a void return function
2void voidfunction() {
3
4}
5
6// Declare a function that returns a value
7String stringfunction() {
8
9 return (String) val;
10}
11
12// Declare a function that takes and returns values
13String parmfunction(String a, String b) {
14
15 return (String) (a + b);
16}
17
18// Use the Functions
19voidfunction();
20String s1 = stringfunction();
21String s2 = parmfunction("Hello", "World!");
2void voidfunction() {
3
4}
5
6// Declare a function that returns a value
7String stringfunction() {
8
9 return (String) val;
10}
11
12// Declare a function that takes and returns values
13String parmfunction(String a, String b) {
14
15 return (String) (a + b);
16}
17
18// Use the Functions
19voidfunction();
20String s1 = stringfunction();
21String s2 = parmfunction("Hello", "World!");
自定义属性:
1// Stand-alone attribute
2[STAThread]
3
4// Attribute with parameters
5[DllImport("ADVAPI32.DLL")]
6
7// Attribute with named parameters
8[DllImport("KERNEL32.DLL",CharSet=CharSet.Auto)]
2[STAThread]
3
4// Attribute with parameters
5[DllImport("ADVAPI32.DLL")]
6
7// Attribute with named parameters
8[DllImport("KERNEL32.DLL",CharSet=CharSet.Auto)]
数组:
1String[] a = new String[3];
2a[0] = "1";
3a[1] = "2";
4a[2] = "3";
5
6String[][] a = new String[3][3];
7a[0][0] = "1";
8a[1][0] = "2";
9a[2][0] = "3";
2a[0] = "1";
3a[1] = "2";
4a[2] = "3";
5
6String[][] a = new String[3][3];
7a[0][0] = "1";
8a[1][0] = "2";
9a[2][0] = "3";
初始化:
1String s = "Hello World";
2int i = 1;
3double[] a = { 3.00, 4.00, 5.00 };
2int i = 1;
3double[] a = { 3.00, 4.00, 5.00 };
If 语句:
1if (Request.QueryString != null) {
2
3}
2
3}
Case 语句:
1switch (FirstName) {
2 case "John" :
3
4 break;
5 case "Paul" :
6
7 break;
8 case "Ringo" :
9
10 break;
11 default:
12
13 break;
14}
2 case "John" :
3
4 break;
5 case "Paul" :
6
7 break;
8 case "Ringo" :
9
10 break;
11 default:
12
13 break;
14}
For 循环:
1for (int i=0; i<3; i++)
2 a(i) = "test";
2 a(i) = "test";
While 循环:
1int i = 0;
2while (i<3) {
3 Console.WriteLine(i.ToString());
4 i += 1;
5}
2while (i<3) {
3 Console.WriteLine(i.ToString());
4 i += 1;
5}
异常处理:
1try {
2 // Code that throws exceptions
3} catch(OverflowException e) {
4 // Catch a specific exception
5} catch(Exception e) {
6 // Catch the generic exceptions
7} finally {
8 // Execute some cleanup code
9}
2 // Code that throws exceptions
3} catch(OverflowException e) {
4 // Catch a specific exception
5} catch(Exception e) {
6 // Catch the generic exceptions
7} finally {
8 // Execute some cleanup code
9}
字符串连接:
1// Using Strings
2String s1;
3String s2 = "hello";
4s2 += " world";
5s1 = s2 + " !!!";
6
7// Using StringBuilder class for performance
8StringBuilder s3 = new StringBuilder();
9s3.Append("hello");
10s3.Append(" world");
11s3.Append(" !!!");
2String s1;
3String s2 = "hello";
4s2 += " world";
5s1 = s2 + " !!!";
6
7// Using StringBuilder class for performance
8StringBuilder s3 = new StringBuilder();
9s3.Append("hello");
10s3.Append(" world");
11s3.Append(" !!!");
事件处理程序委托:
1void MyButton_Click(Object sender,
2 EventArgs E) {
3
4}
2 EventArgs E) {
3
4}
声明事件:
1// Create a public event
2public event EventHandler MyEvent;
3
4// Create a method for firing the event
5protected void OnMyEvent(EventArgs e) {
6 MyEvent(this, e);
7}
2public event EventHandler MyEvent;
3
4// Create a method for firing the event
5protected void OnMyEvent(EventArgs e) {
6 MyEvent(this, e);
7}
向事件添加事件处理程序或从事件移除事件处理程序:
1Control.Change += new EventHandler(this.ChangeEventHandler);
2Control.Change -= new EventHandler(this.ChangeEventHandler);
2Control.Change -= new EventHandler(this.ChangeEventHandler);
强制类型转换:
1MyObject obj = (MyObject)Session["Some Value"];
2IMyObject iObj = obj;
2IMyObject iObj = obj;
转换:
1int i = 3;
2String s = i.ToString();
3double d = Double.Parse(s);
2String s = i.ToString();
3double d = Double.Parse(s);
带继承的类定义:
1using System;
2
3namespace MySpace {
4
5 public class Foo : Bar {
6
7 int x;
8
9 public Foo() { x = 4; }
10 public void Add(int x) { this.x += x; }
11 override public int GetNum() { return x; }
12 }
13
14}
15
16// csc /out:librarycs.dll /t:library
17// library.cs
2
3namespace MySpace {
4
5 public class Foo : Bar {
6
7 int x;
8
9 public Foo() { x = 4; }
10 public void Add(int x) { this.x += x; }
11 override public int GetNum() { return x; }
12 }
13
14}
15
16// csc /out:librarycs.dll /t:library
17// library.cs
实现接口:
1public class MyClass : IEnumerable {
2
3
4 IEnumerator IEnumerable.GetEnumerator() {
5
6 }
7}
2
3
4 IEnumerator IEnumerable.GetEnumerator() {
5
6 }
7}
带 Main 方法的类定义:
1using System;
2
3public class ConsoleCS {
4
5 public ConsoleCS() {
6 Console.WriteLine("Object Created");
7 }
8
9 public static void Main (String[] args) {
10 Console.WriteLine("Hello World");
11 ConsoleCS ccs = new ConsoleCS();
12 }
13
14}
15
16// csc /out:consolecs.exe /t:exe console.cs
2
3public class ConsoleCS {
4
5 public ConsoleCS() {
6 Console.WriteLine("Object Created");
7 }
8
9 public static void Main (String[] args) {
10 Console.WriteLine("Hello World");
11 ConsoleCS ccs = new ConsoleCS();
12 }
13
14}
15
16// csc /out:consolecs.exe /t:exe console.cs
标准模块:
1using System;
2
3public class Module {
4
5public static void Main (String[] args) {
6 Console.WriteLine("Hello World");
7}
8
9}
10// csc /out:consolecs.exe /t:exe console.cs
2
3public class Module {
4
5public static void Main (String[] args) {
6 Console.WriteLine("Hello World");
7}
8
9}
10// csc /out:consolecs.exe /t:exe console.cs