`
pqcc
  • 浏览: 125981 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

C#学习笔记

阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics