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

來源:互聯網
上載者:User
筆記 最近學習了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 {}


相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。