首页 > 开发 > 综合 > 正文

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

2024-07-21 02:27:58
字体:
来源:转载
供稿:网友
最近学习了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,


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表