c#语法糖253

原文章地址

  1. abstract类最好用来声明公共const变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Bad
{
abstract class MathConstants
{
public static readonly double Pi = 3.14;
}
class Circle : MathConstants
{
private double radius;
public double Area() => Math.Pow(radius, 2) * Pi;
}
}
  1. ReadOnlyCollection替换Arrayreadonly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//错误
class Bad
{
public static readonly string[] Foo = { "hello", "world" };
public static void Main(string[] args)
{
Foo[0] = "goodbye";
}
}
//对的
class Good
{
public static readonly ReadOnlyCollection<string> Foo
= new ReadOnlyCollection<string>(new string[] { "hello", "world" });
public static void Main(string[] args)
{
}
}
  1. Assembly的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class AssemblyPathInjectionHandler : IHttpHandler {
public void ProcessRequest(HttpContext ctx) {
string configType = ctx.Request.QueryString["configType"];
if (configType.equals("configType1") || configType.equals("configType2")) {
// GOOD: Loaded assembly is one of the two known safe options
var safeAssembly = Assembly.LoadFile(@"C:\SafeLibraries\" + configType + ".dll");
// Code execution is limited to one of two known and vetted assemblies
MethodInfo m = safeAssembly.GetType("Config").GetMethod("GetCustomPath");
Object customPath = m.Invoke(null, null);
// ...
}
}
}
  1. Cyclomatic Complexity(圈复杂度)

  2. dynamic 弱语言的东西类似js的结构体,和模板搭配很好用

  3. Obsolete

    1
    2
    [Obsolete("该方法已被弃用,请使用NewMethod代替")]
    static void OldMethod()
  4. abstract属性可以通过继承覆盖
    ···
    public abstract class Shape
    {
    private string name;

    public Shape(string s)
    {

    // calling the set accessor of the Id property.
    Id = s;
    

    }
    public string Id
    {

    get
    {
        return name;
    }
    
    set
    {
        name = value;
    }
    

    }

    // Area is a read-only property - only a get accessor is needed:
    public abstract double Area
    {

    get;
    

    }

    public override string ToString()
    {

    return $"{Id} Area = {Area:F2}";
    

    }
    }
    public class Square : Shape
    {
    private int side;

    public Square(int side, string id)

    : base(id)
    

    {

    this.side = side;
    

    }

    public override double Area
    {

    get
    {
        // Given the side, return the area of a square:
        return side * side;
    }
    

    }
    }

···

  1. is的规则

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    interface Animal { }
    class Cat : Animal { }
    class Dog : Animal { }
    static void Main(string[] args)
    {
    List<Animal> animals = new List<Animal> { new Cat(), new Dog() };
    foreach (Animal a in animals)
    {
    if (a is Cat)
    Console.WriteLine("Miaow!");
    if (a is Dog)
    Console.WriteLine("Woof!");
    if (a is Animal)
    Console.WriteLine("Animal!");
    }
    Console.ReadLine();
    Console.ReadKey();
    }// Miaow Animal Woof Animal
  2. 引用用类型,值类型比较

引用类型:
ReferenceEquals(null, x)比较是否是指向同一内存
虚函数Equals

值类型:
==运算符比较

  1. 双重检查lock
// //