前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii
C# 7 More Effective C# C# C# C# C# C# Common Language Runtime CLR just-in-time JIT C# 1 API 2 C# lazy evaluation composable interfaces Task API 3 viii
task 4 multithreaded parallel execution PLINQ CPUs 5 C# dynamic language C# C# 5 6 C# try/finally using using System; using static System.Console; using System.Collections.Generic; using System.Linq; using System.Text; ix
處理資料型別 1 C# passive data containers 01 C# C# getter setter auto properties expression-bodied public get set public (x, y) 1
1 public accessors.net Framework public.net Framework public WPF Windows Forms Web Forms reflection named property textboxcity.databindings.add("text", address, nameof(city)); textboxcity Text address City City public Framework public Framework UI UI public Name public class Customer private string name; public string Name get => name; set if (string.isnullorwhitespace(value)) throw new ArgumentException( "Name cannot be blank", 2
01 nameof(name)); name = value; // public get set 39 public class Customer private object synchandle = new object(); private string name; public string Name get lock (synchandle) return name; set if (string.isnullorempty(value)) throw new ArgumentException( "Name cannot be blank", nameof(name)); lock (synchandle) name = value; // virtual public class Customer 3
1 public virtual string Name get; set; implicit property backing store getters setters C# private get set abstract public interface INameValuePair<T> string Name get; T Value get; set; ref out C# get set public class Customer public virtual string Name 4
01 get; protected set; // indexers sequence public int this[int index] get => thevalues[index]; set => thevalues[index] = value; private int[] thevalues = new int[100]; // int val = someobject[i]; single-item properties virtual abstract public Address this[string name] get => addressvalues[name]; set => addressvalues[name] = value; private Dictionary<string, Address> addressvalues; C# public int this[int x, int y] => ComputeValue(x, y); public int this[int x, string name] => ComputeValue(x, name); 5
1 this C# virtual abstract setters getters C# // public public class Customer public string Name; // get set string name = customerone.name; customerone.name = "This Company, Inc."; Name Microsoft Microsoft Intermediate Language MSIL public public public C# assemblies assembly 6
01 assembly MSIL just-in-time JIT JIT get set public protected dictionaries private 7
1 02 C# C# public string Name get; set; backing field set public string Name get; protected set; // public string Name get; internal set; // public string Name get; protected internal set; // 8
02 public string Name get; private set; // // public string Name get; C# virtual override virtual virtual derived overrides get set virtual public class BaseType public virtual string Name get; protected set; public class DerivedType : BaseType public override string Name get => base.name; protected set if (!string.isnullorempty(value)) base.name = value; 9
1 C# private // public class Person public string FirstName get; set; public string LastName get; set; public override string ToString() => $"FirstName LastName"; // public class Person public Person(string firstname, string lastname) // setters this.firstname = firstname; this.lastname = lastname; private string firstname; public string FirstName get => firstname; set if (string.isnullorempty(value)) throw new ArgumentException( "First name cannot be null or empty"); firstname = value; 10
02 private string lastname; public string LastName get => lastname; private set if (string.isnullorempty(value)) throw new ArgumentException( "Last name cannot be null or empty"); lastname = value; public override string ToString() => $"FirstName LastName"; Serializable attribute 11