• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#基础(更新中)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Data Structure

There're two types of variables in C#, reference type and value type.
Enum:

enum Color{
Red=0,
Green=1
}

//equals to 
enum Color{
Red,//start from 0 as default
Green
}

int to enum:(Color)val

Arrays and Collections

Array

declare array:

new int[10]
new int[10]{xx,xx.....};
new int[]{aa,aa};

Collection

List<string> theList=new List<string>();//can use value type here, not only reference type
theList.Count;//size of the list

class

when calling same name class with different namespace, it will use class of same namespace first, then System namespace.

fields and properties

  1. can't use var to declare fields like var count=0,must use int count=0
  2. const is static, so we can't use them together like public static const int count=0
  3. rules of naming fields and property: first letter with uppercase is public, first letter with lowercase is private.

Auto-implemented property: remove their private fields.
initiate with default: public MyClass val {get;set;} = new MyClass();

anonymous type

for the cases:

  • only use in one function and not need for other functions
  • only exists for only short time, and will be stored to other places

var val=new {name='Alex', age=12};

Extensions

class TheExtensionClass{
    public int toInt(this string value){
        return int.Parse(value);
    }
}

//so that the function can be called on string
str.toInt();

function

out, in and ref

if use out/in/ref for value type variable, then it will convert to reference type.
must initiate out variable in the function before use it.

default parameter and named parameter

Default parameter

  • must declared after non-default parameter
  • if we don't want to give value to the first default parameter but want to give the second default parameter, we can't do this without named parameter

Named parameter:
can call the function and break the sequence of parameters, especially for default parameter

special function:lambda

for the cases:

  • short function
var res=theList.Find(MyFunc);
boolean MyFunc(Student aStudent){
    return studentName="abc";
}

theList.Find(student=>{
    return studentName="abc";
});

theList.Find(student=> studentName="abc");

Event and asynchronous programming

Event

for the cases:
when executing monitor function, it will automatically execute the monitored function

  1. declare Action variable in class A:Action action
  2. binding the action variable to f2 (monitored function) of class B:action+=f2
  3. trigger the event: in f3(monitor function) call the action with action();
    ### asynchronous programming
    like event
    RequestSupport(CallBackFunc)

Exception and log

Exception

try{
}catch{
}
try{
}catch(FormatException){
}
try{
}catch(FormatException e){
    print e;
    print e.Message;
}

Log


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#第一个多窗口的程序,EffectiveKey发布时间:2022-07-13
下一篇:
C# 找出泛型集合中的满足一定条件的元素 List.Wher()发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap