能够从初始化语句的右边推断出类型,e.g.
var total = 15;
var mec = new MyExcellentClass();
使用var的一些条件
不管嵌套级别如何,都不能在第一个名称的有效范围内声明另一个同名的本地变量。
void listInts(params int[] inVals)
c.Calc(c:2, a:4, b:3);
public int myValue
{
set
{
SetAccessCode
}
get
{
GetAccessCode
}
}
与成员变量类似:
与成员变量有区别:
set访问器总是:
get访问器总是:
只读和只写属性:
自动实现属性:
class C1
{
public int MyValue
{
set;get;
}
}
静态属性:
new TypeName {FieldOrProp=InitExpr, FieldOrProp=InitExpr,...}
new TypeName(ArgList) {FieldOrProp=InitExpr, FieldOrProp=InitExpr,...}
允许你在创建新的对象实例时,设置字段和属性的值。
- 初始化的字段和属性必须是public;
- 初始化发生在构造函数执行之后;
成员变量可以用readonly修饰符声明,其作用类似于const,一旦被设定就不能改变。
可以在下列任意位置设置它的值;
readonly字段的值可以在运行时决定;
string this [int index, ...]
{
set
{
SetAccessCode
}
get
{
GetAccessCode
}
}
class Person
{
public string name {get; private set;}
}
base.Field1
public MyDerivedClass(int x, string y):base(x, y)
public MyClass(int x):this(x, "using default string")
abstract public void PrintStuff(string s);
abstract public int MyProperty
{
get;
set;
}
namespace ExtensiionMethods
{
sealed class MyData
{
private double D1, D2, D3;
public double Sum(){return D1+D2+D3;}
}
static class ExtendMyData
{
public static double Average(this MyData md)
{
return md.Sum() / 3;
}
}
class Program
{
static void Main()
{
MyData md = new MyData();
md.Average();
}
}
}
public和static修饰符是必须的
隐式转换
public static implicit operator TargetType (SourceType Identifier)
{
...
return ObjectOfTargetType;
}
显示转换
public static explicit operator TargetType (SourceType Identifier)
{
...
return ObjectOfTargetType;
}
public和static修饰符是必须的
public static LimitedInt operator +(LimitedInt x, double y)
System.Type t = typeof(SomeClass);
do
...
while(...);
using管理的资源必须实现System.IDisposable接口
using (ResourceType Id1 = Expr1, Id2 = Expr2, ...) EmbeddedStatement
结构与类的区别:
enum TrafficLight : ulong
{
...
}
enum CardDeckSettings : uint
{
SingleDeck = 0x01, //位0
LargePictures = 0x02, //位1
FancyNumbers = 0x04, //位2
Animation = 0x08 //位3
}
CardDeckSettings ops = CardDeckSettings.SingleDeck
| CardDeckSettings.FancyNumbers
| CardDeckSettings.Animation;
//判断标志字是否包含特定的位标志集
bool useFancyNumbers = ops.HasFlag(CardDeckSettings.FancyNumbers);
int x = myArray[4,6,1];
myArray[2][7][4]
int[] array1; //一维数组
int[,] array2; //二维数组
int[,,] array3; //三维数组
int arr1 = new int[5];
int[,,] arr2 = new int[3,6,2];
不用输入维度长度,编译器可以通过初始化值的个数来推断长度
int[,] arr = new int[,]{{1,2},{3,4},{5,6}};
int[][] arr = new int[3][4]//error
每个子数组要单独创建
int[][] arr = new int[3][];
arr[0] = new int[] {1,2,3};
arr[1] = new int[] {1,2};
arr[2] = new int[] {1,2,3,4,5};
子数组可以是矩形数组
int [][,] arr = new int[3][,];
arr[0] = new int[,]{{1,2},{3,4}};
...
delegate void MyDel(int value); //声明委托类型
class Program
{
void PrintLow(int value){...}
void PrintHigh(int value){...}
static void Main()
{
Program program = new Program();
MyDel del; //声明委托变量
del = new MyDel(program.PrintLow);
//del = program.PrintLow; 另一种简洁写法
del(10); //执行委托
}
}
MyDel delA = myInstObj.Fun;
MyDel delB = SClass.Fun;
MyDel delC = delA + delB;
delA += delB;
delA -= SClass.Fun;
class Program
{
delegate int Del(int InParam);
static void main()
{
Del del = delegate(int x)
{
return x + 20;
}
}
}
例子:
delegate void Handler(); //声明委托
class Increment
{
public event Handler CountedADozen; //创建事件并发布
public void DoCount()
{
for(int i = 0; i < 100; ++i)
if(i % 12 == 0 && CountedADozen != null)
CountedADozen(); //触发事件
}
}
class Dozens
{
public int DozensCount {get; private set;}
public Dozens(Incrementer incrementer)
{
DozensCount = 0;
incrementer.CountedADozen += IncrementDozensCount; //订阅事件
}
//事件处理程序
void IncrementDozensCount()
{
DozensCount++;
}
}
class Program
{
static void Main()
{
Incrementer incrementer = new Incrementer();
Dozens dozensCounter = new Dozens(incrementer);
incrementer.Docount();
}
}
class MyClass: IIfc1, IIfc2
{
void IIfc1.PrintOut(string s) {...}
void IIfc2.PrintOut(string s) {...}
}
checked(表达式);
unchecked(表达式);
checked
{
...
}
unchecked
{
...
}
Expr is TargetType
如果Expr可以通过以下方式成功转换为目标类型,运算符返回true
- 引用转换
- 装箱转换
- 拆箱转换
where TypeParam: constraint, constraint, ...
e.g.
class MyClass
where T2:Customer
where T3:IComparable
{
...
}
共有五种类型的约束:
where子句可以以任何次序列出,然而,where子句种的约束必须有特定的顺序
public void PrintDataT>(S p, T t) where S:person
{
...
}
//产生枚举器的迭代器
public IEnumerator IteratorMethod()
{
...
yield return ...;
}
//产生可枚举类型的迭代器
public IEnumerable IteratorMethod()
{
...
yield return ...;
}
可以给命名空间或命名空间内的类型起别名;
using Syst = System;
using SC = System.Console;
–
#pragma warning disable 615,414 //关闭部分警告
#pragma warning restore 618 //重新开启部分警告
#pragma warning disable //关闭所有警告
#pragma warning restore //重新开启所有警告
class Program
{
//[Obsolete("Use method SuperPrintOut", true)] //true表示被标记为错误而不仅是警告
[Obsolete("Use method SuperPrintOut")]
static void PrintOut(string str)
{
...
}
static void Main(string[] args)
{
PrintOut("xxx");
}
}
#define DoTrace
usig System;
class Program
{
[Conditional("DoTrace")]
static void TraceMessage(string str)
{
Console.WriteLine(str);
}
static void Main()
{
TraceMessage("Start of Main");
Console.WriteLine("Doing work in Main");
TraceMessage("End of Main");
}
}
如果DoTrace被定义,那么编译器会包含所有对TraceMessage的调用代码;否则,不会调用。
using System;
using System.Runtime.CompilerServices;
public static class Program
{
public static void MyTrace(string message,
[CallerFilePath] strig filename = "",
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string callingMember = "")
{
...
}
public static void Main()
{
MyTrace("Simple message");
}
}