System.Activator
public class C { public C() { … } }
public class C { public static C NewC() { return new C(); } }
private
base()
this()
class PersonFactory { public Person MakePerson( int age) { if (age < 18) return new Minor(); else return new Adult(); } }
string.Format( “
XElement(“person”, XAttribute(“name”, name)) .ToString();
StringBuilder AppendLine()
AppendFormat()
Util.AppendFormatLine(stringBuilder, format, params) stringBuilder.AppendFormatLine(format, params)
stringBuilder.AppendFormat(“… {0}”, params, Environment.NewLine)
MemberwizeClone
ICloneable Clone()
ToString() GetHashCode()
[Serializable] public abstract class IPrototype
{ public T DeepCopy() { MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, this); stream.Seek(0, SeekOrigin.Begin); T copy = (T)formatter.Deserialize(stream); stream.Close(); return copy; } }
DeepCopy IPrototype
WeakReference
new
public class C { private C() { /* nothing here */ } class CMaker { static CMaker () { } internal static readonly C instance = new C(); } public static C Instance { get { return CMaker.instance; } } }
x:Static
Random class RandomGenerator { public Random GetRandom() { return new Random(); // not what I want } }
interface IRandom { int RandomNumber(); }
RandomGenerator IRandom
class RandomAdapter: RandomGenerator, IRandom { int RandomNumber() { return GetRandom().Next(); } }
– RandomGenerator rg = new RandomGenerator(); Random r = rg.GetRandom(); – IRandom rand = new Adapter(); int n = rand.RandomNumber();
– class C { … } – class CCollection : Collection { … } class CContainer { private Collection items; }
class Neuron { … } class Layer : Collection { … } neuron.ConnectTo(otherNeuron); neuron.ConnectTo(layer); layer.ConnectTo(neuron); layer.ConnectTo(otherLayer);
– IEnumerable yield return this; ICollection
Card
CardInPlay
♣ ♠ ♥ ♦
Rank Suit
Rank Suit Orientation
IA A
IB B
→ →
new
Bitmap byte[]
Reflection.Emit
XElement.Parse →
IEnumerable yield return GetEnumerator()
AsyncEnumerator
→ →
interface ICarState { void Go(Context ctx); // Drive? }
class Context { ICarState state; public void GoGoGo() { state.Go(this); state.Go(this); // yeah, right :) state.Go(this); } }
class CrashedState : ICarState { void Go(Context ctx) { MessageBox.Show( “Are you crazy?”); } }
class MovingState : ICarState { void Go(Context ctx) { // driving // is that a rock? // CRASH! ctx.state = new CrashedState();
The state just changed!
} }
interface IStrategy { void Evaluate(Context ctx); }
SingleEvaluator, ParralelEvaluator, GpuEvaluator
class Context { IStrategy strategy; Matrix m1, m2; static void Multiply(Matrix m1, Matrix m2) { this.m1 = m1; this.m2 = m2; if (strategy == null) { // on-demand if (gpu.pixelShader >= 2.0) strategy = new GpuStrategy(); else if (Environment.ProcessorCount > 1) strategy = new ParallelStrategy(); else strategy = new SingleStrategy(); } strategy.Evaluate(this); } }
→ →
StringBuilder