程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#學習筆記之一(basic, class, function,array, collection interface)

C#學習筆記之一(basic, class, function,array, collection interface)

編輯:關於C語言
最近學習了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 {}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved