C# Basic
变量声明:
注释 :
访问索引属性:
声明索引属性:
声明简单属性:
声明和使用枚举:
枚举集合:
声明和使用方法:
自定义属性:
数组:
初始化:
If 语句:
Case 语句:
For 循环:
While 循环:
异常处理:
字符串连接:
事件处理程序委托:
声明事件:
向事件添加事件处理程序或从事件移除事件处理程序:
强制类型转换:
转换:
带继承的类定义:
实现接口:
带 Main 方法的类定义:
标准模块:
1
int x;
2
String s;
3
String s1, s2;
4
Object o;
5
Object obj = new Object();
6
public String name;
int x;2
String s;3
String s1, s2;4
Object o;5
Object obj = new Object();6
public String name;语句:
1
Response.Write("foo");
Response.Write("foo");注释 :
1
// This is a comment
2![]()
3
/*
4
This
5
is
6
a
7
multiline
8
comment
9
*/
// This is a comment2

3
/*4
This5
is6
a7
multiline8
comment9
*/访问索引属性:
1
String s = Request.QueryString["Name"];
2
String value = Request.Cookies["key"];
String s = Request.QueryString["Name"];2
String value = Request.Cookies["key"];声明索引属性:
1
// Default Indexed Property
2
public String this[String name] {
3
get {
4
return (String) lookuptable[name];
5
}
6
}
// Default Indexed Property2
public String this[String name] {3
get {4
return (String) lookuptable[name];5
}6
}声明简单属性:
1
public String name {
2
get {
3
![]()
4
return
;
5
}
6![]()
7
set {
8
= value;
9
}
10
}
public String name {2
get {3

4
return
;5
}6

7
set {8
= value;9
}10
}声明和使用枚举:
1
// Declare the Enumeration
2
public enum MessageSize {
3![]()
4
Small = 0,
5
Medium = 1,
6
Large = 2
7
}
8![]()
9
// Create a Field or Property
10
public MessageSize msgsize;
11![]()
12
// Assign to the property using the Enumeration values
13
msgsize = Small;
// Declare the Enumeration2
public enum MessageSize {3

4
Small = 0,5
Medium = 1,6
Large = 27
}8

9
// Create a Field or Property10
public MessageSize msgsize;11

12
// Assign to the property using the Enumeration values13
msgsize = Small;枚举集合:
1
foreach ( String s in coll ) {
2
![]()
3
}
foreach ( String s in coll ) {2

3
}声明和使用方法:
1
// Declare a void return function
2
void voidfunction() {
3
![]()
4
}
5![]()
6
// Declare a function that returns a value
7
String stringfunction() {
8
![]()
9
return (String) val;
10
}
11![]()
12
// Declare a function that takes and returns values
13
String parmfunction(String a, String b) {
14
![]()
15
return (String) (a + b);
16
}
17![]()
18
// Use the Functions
19
voidfunction();
20
String s1 = stringfunction();
21
String s2 = parmfunction("Hello", "World!");
// Declare a void return function2
void voidfunction() {3

4
}5

6
// Declare a function that returns a value7
String stringfunction() {8

9
return (String) val;10
}11

12
// Declare a function that takes and returns values13
String parmfunction(String a, String b) {14

15
return (String) (a + b);16
}17

18
// Use the Functions19
voidfunction();20
String s1 = stringfunction();21
String 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)]
// Stand-alone attribute2
[STAThread]3

4
// Attribute with parameters5
[DllImport("ADVAPI32.DLL")]6

7
// Attribute with named parameters8
[DllImport("KERNEL32.DLL",CharSet=CharSet.Auto)]数组:
1
String[] a = new String[3];
2
a[0] = "1";
3
a[1] = "2";
4
a[2] = "3";
5![]()
6
String[][] a = new String[3][3];
7
a[0][0] = "1";
8
a[1][0] = "2";
9
a[2][0] = "3";
String[] a = new String[3];2
a[0] = "1";3
a[1] = "2";4
a[2] = "3";5

6
String[][] a = new String[3][3];7
a[0][0] = "1";8
a[1][0] = "2";9
a[2][0] = "3";初始化:
1
String s = "Hello World";
2
int i = 1;
3
double[] a = { 3.00, 4.00, 5.00 };
String s = "Hello World";2
int i = 1;3
double[] a = { 3.00, 4.00, 5.00 };If 语句:
1
if (Request.QueryString != null) {
2
![]()
3
}
if (Request.QueryString != null) {2

3
}Case 语句:
1
switch (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
}
switch (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
}For 循环:
1
for (int i=0; i<3; i++)
2
a(i) = "test";
for (int i=0; i<3; i++)2
a(i) = "test";While 循环:
1
int i = 0;
2
while (i<3) {
3
Console.WriteLine(i.ToString());
4
i += 1;
5
}
int i = 0;2
while (i<3) {3
Console.WriteLine(i.ToString());4
i += 1;5
}异常处理:
1
try {
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
}
try {2
// Code that throws exceptions3
} catch(OverflowException e) {4
// Catch a specific exception5
} catch(Exception e) {6
// Catch the generic exceptions7
} finally {8
// Execute some cleanup code9
}字符串连接:
1
// Using Strings
2
String s1;
3
String s2 = "hello";
4
s2 += " world";
5
s1 = s2 + " !!!";
6![]()
7
// Using StringBuilder class for performance
8
StringBuilder s3 = new StringBuilder();
9
s3.Append("hello");
10
s3.Append(" world");
11
s3.Append(" !!!");
// Using Strings2
String s1;3
String s2 = "hello";4
s2 += " world";5
s1 = s2 + " !!!";6

7
// Using StringBuilder class for performance8
StringBuilder s3 = new StringBuilder();9
s3.Append("hello");10
s3.Append(" world");11
s3.Append(" !!!");事件处理程序委托:
1
void MyButton_Click(Object sender,
2
EventArgs E) {
3![]()
![]()
4
}
void MyButton_Click(Object sender,2
EventArgs E) {3


4
}声明事件:
1
// Create a public event
2
public event EventHandler MyEvent;
3![]()
4
// Create a method for firing the event
5
protected void OnMyEvent(EventArgs e) {
6
MyEvent(this, e);
7
}
// Create a public event2
public event EventHandler MyEvent;3

4
// Create a method for firing the event5
protected void OnMyEvent(EventArgs e) {6
MyEvent(this, e);7
}向事件添加事件处理程序或从事件移除事件处理程序:
1
Control.Change += new EventHandler(this.ChangeEventHandler);
2
Control.Change -= new EventHandler(this.ChangeEventHandler);
Control.Change += new EventHandler(this.ChangeEventHandler);2
Control.Change -= new EventHandler(this.ChangeEventHandler);强制类型转换:
1
MyObject obj = (MyObject)Session["Some Value"];
2
IMyObject iObj = obj;
MyObject obj = (MyObject)Session["Some Value"];2
IMyObject iObj = obj;转换:
1
int i = 3;
2
String s = i.ToString();
3
double d = Double.Parse(s);
int i = 3;2
String s = i.ToString();3
double d = Double.Parse(s);带继承的类定义:
1
using System;
2![]()
3
namespace 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
using System;2

3
namespace 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:library17
// library.cs实现接口:
1
public class MyClass : IEnumerable {
2
![]()
3![]()
4
IEnumerator IEnumerable.GetEnumerator() {
5
![]()
6
}
7
}
public class MyClass : IEnumerable {2

3

4
IEnumerator IEnumerable.GetEnumerator() {5

6
}7
}带 Main 方法的类定义:
1
using System;
2![]()
3
public 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
using System;2

3
public 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标准模块:
1
using System;
2![]()
3
public class Module {
4![]()
5
public static void Main (String[] args) {
6
Console.WriteLine("Hello World");
7
}
8![]()
9
}
10
// csc /out:consolecs.exe /t:exe console.cs
using System;2

3
public class Module {4

5
public static void Main (String[] args) {6
Console.WriteLine("Hello World");7
}8

9
}10
// csc /out:consolecs.exe /t:exe console.cs

浙公网安备 33010602011771号