C#学习笔记之一(basic, class, function,array, collection interface)

类别:.NET开发 点击:0 评论:0 推荐:

最近学习了C#程序设计的课程, 现在将笔记总结如下, 没有系统整理,都是课上记得notes, 后面几部分以程序居多,因为这些笔记没有做过整理,所以很多code没有详细的注释,如果有时间的话,我会对笔记做系统的整理,还有这里没有提及基本的语法介绍,望大家谅解:

Basic:
 //利用out, 不用给i,j assign初始值
 int i,j;
 f(out i, out j) {}
  //using, 括号外自动destroy对象
 using (Font theFont == new Font("Ariel", 10.0f)){}
 //constants
 const int i = 32;
 //Enumeration
 enum E {a=5,b=8}
 int i = (int) E.a;
 //turn buff data to string
 Encoding.ASCII.GetString(buff, 0, bytesRead) 
 // read data
 string s = Console.ReadLine()
 int j= Convert.ToInt32(s); 
 //get textbox value and convert to double
 double left = double.Parse(textBox1.Text);
 Application.DoEvent(); //this will let app deal with event
array:
 A[] a = new A[5]; // a[0]to a[5] is null
 int[] arr = new int[2] {1,2}
 int[] arr = {2,3,4,5}
 foreach(A a in arr_A) {}
 for(int i=0; i<arr.Length; i++) {}
 //Rectangular Arrays
 int[,] rectArray = new int[4,5]
 int[,] arr = {{1,2,3,4},{1,2,3,4}{1,2,3,4}}; //init
 //Jagged array, 数组中包含数组,各维不等长
 // ja[0].Length is length of ja[0]
 int [][] ja = new int[4][];
 //using params transfer a array to a function
 //using f(1,2,3,4) to call the function
 f(params int[] values){
  foreach (int[] a in values) {}
 } 
Collection interface:
 indexers:
  //define a indexers
  public Myclass this[int offset] {get{};set{}}
  //call it
  Employee joe = bostonOffice["Joe"];
 IEnumerable:
  //return a IEnumerator object
  GetEnumerator()
 IEnumerator:
    Reset(){}
         Current(){}
  MoveNext(){} 
 IComparable:
  //Class inherit IComparable interface
  //Implement CompareTo() method
  CompareTo()
 ArrayLists:
  Count  Get number of elements
  Add()  Add an object
  Clear() Remove all objects
  Reverse() Reverse order of elements
  Sort()  Sort the elements 
function:
 //default by value
 f(int x){}
 //by reference
 f(ref int x){}
class:
  //存取:
 public, private, protected, internal, protected internal
 this, static //static member must be init in class
  //继承
 class A: B {}
  //多态  
 virtual,override
 //init对象
      Employee emp1 = new Employee();

     //先隐式转换3为Roman
     Roman r4 = r1 + 3;
     //先显式转换7.5为Roman
 Roman r5 = r1 +(Roman)7.5;  

 // 运算符重载,先返回int, 然后用隐式转换
 public static Roman operator+ (Roman l, Roman r)
 {
  return (l.val+r.val);
 }
 //显式转换
 public static explicit operator Roman(float val)
 {
  return new Roman(val);
 }
 //隐式转换
 public static implicit operator Roman(int val)
 {
  return new Roman(val);
 }
  //properties, 注意大小写
 public int Age {
  get {return age;}
  set {age = value;}
 }
 
 //Interface
 //interface methods must be implement in class
 public interface IA {}
 //is 测试类是否从接口继承
 if (a is IA) {IA c = (IA) a; }
 //as 测试类是否从接口继承
 IA c = a as IA;
 if (c != null) {}
 //interface properties
 pubiic interface IAA: IA
 {
  float F{get;set;}
 }
 //mutiface interface inheritance
 public class C: IA, IB {}

本文地址:http://com.8s8s.com/it/it45103.htm