1. 聲明變量:
int x;
String s1, s2;
Object o;
Object obj = new Object();
public String name;
2. 發出語句:
Response.Write("foo");
3. 註釋:
// 單行註釋
/**/ 多行註釋
4. 訪問索引屬性:
String s = Request.QueryString("Name");
String value = Request.Cookies["key"];
5. 聲明索引屬性:
public String this[String name]
{
return (String)lookuptable[name];
}
6. 聲明簡單屬性:
public String name
{
get
{...
return ...;
}
set{
...= value;
}
}
7. 使用和聲明枚舉:
//Declare the Enumeration
public enum MessageSize{
Small = 0;
Medium = 1;
Large = 2;
}
// Create a Field or Property
public MessageSize msgsize;
// Assign to the property using the Enumeration values
msgsize = Small;
8. 枚舉集合
foreach(String s in coll)
{
...
}
9. 聲明和使用方法:
// Declare a void return function
void function()
{
...
}
// Declare a function that returns a value
String function()
{
...
return (String) val;
}
String paramfunction(String a, String b){
...
return (String)(a+b);
}
// use the functions
String s1 = stringfunction();
String s2 = paramfunction("Hello","World!");
10. 自定義屬性:
//Stand-alone attribute
[STAThread]
//Attribute with parameters
[DllImport("ADVAPI32.DLL",CharSet=CharSet.Auto)]
11. 數組:
String[] a = new String[3];
a[0] = "1";
a[1] = "2";
String[][] a = new String[3][3];
a[0][0] = "1";
12. 初始化:
String s = "Hello World";
int i = 1;
double[] a = {3.00,4.00,5.00}
13. If 語句:
if(Request.QueryString != null){
...
}
14. Case 語句:
switch(FirstName){
case "John":
...
break;
case ""
}
15. for 循環:
for(int i=0;i<3;i++)
{
a(i) = "test";
}
16. while循環:
int i = 0;
while(i<3)
{
Console.WriteLine(i.ToString());
i +=1;
}
17. 異常處理
try{
//Code that throws exceptions
}catch(OverflowException e){
// Catch a specific exception
catch(Exception e){
//Catch the generic exceptions
}finally{
// Execute some cleanup code
}
18. 字符串連接:
(1). +號
(2). StringBuilder s3 = new StringBuilder();
s3.Append("hello");
19. 事件處理程序委託
void MyButton_Click(Object sender,EventArgs E){
...
}
20. 聲明事件:
//Create a public event
public event EventHandler MyEvent;
//Create a method for firing the event
protected void OnMyEvent(EventArgs e){
MyEvent(this,e);
}
向事件添加事件處理程序或從事件移除事件處理程序
Control.Change += new EventHandler(this.ChangeEventHandler);
Control.Change += new EventHandler(this.ChangeEventHandler);
21. 強制類型轉換:
MyObject obj = (MyObject)Session["Some Value"];
IMyObject iObj = obj;
22. 轉換:
int i=3;
String s = i.ToString();
double d = Double.Parse(s);
23. 帶繼承的類定義:
using System;
namespace MySpace{
public class Foo:Bar{
int x;
public Foo() { x=4;}
public void Add(int x)
{
this.x += x;
}
override public int GetNum() { return x;}
}
}
// csc/out:liberarycs.dll /t:library
// library.cs
24. 實現接口:
public class MyClass:IEnumerable{
...
IEnumerator Ienumerable.GetEnumerator(){
...
}
}
25. 帶 main 方法的類定義:
using System;
public class ConsoleCS{
public ConsoleCS()
{
Console.WriteLine("Object Created");
}
public static void Main(String args[]){
Console.WriteLine("Hello World");
ConsoleCS ccs = new ConsoleCS();
}
}
// csc/out:consolecs.exe /t:exe console.cs
26. 標準模塊:
using System;
public class Module{
public static void Main(String[] args){
Console.WriteLine("Hello World");
}
}
// csc/out:consolecs.exe /t:exe console.cs