📄 cessentials_examples.htm
字号:
<p><pre>if (l is URL) ((URL)l).Navigate();</pre><p><b>Polymorphism</b><p><pre>class LocalFile : Location { public void Execute() { Console.WriteLine("Executing "+Name); } // The constructor for LocalFile, which calls URL's constructor public LocalFile(string name) : base(name) {}}class Test { static void Main() { URL u = new URL(); LocalFile l = new LocalFile(); Show(u); Show(l); } public static void Show(Location loc) { Console.Write("Location is: "); loc.Display(); }}</pre><p><b>Virtual Function Members</b><p><pre>class Location { public virtual void Display() { Console.WriteLine(Name); } ...}class URL : Location { // chop off the http:// at the start public override void Display() { Console.WriteLine(Name.Substring(6)); } ...}</pre><p><b>Abstract Classes and Abstract Members</b><p><pre>abstract class Location { public abstract void Launch();}class URL : Location { public override void Launch() { Console.WriteLine("Run Internet Explorer..."); }}class LocalFile : Location { public override void Launch() { Console.WriteLine("Run Win32 Program..."); }}</pre><p><b>Sealed Classes</b><p><pre>sealed class Math { ...}</pre><p><b>Hiding Inherited Members</b><p><pre>class B { public virtual void Foo() {}}class D : B { public override void Foo() {}}class N : D { public new void Foo() {} // hides D's Foo}N n = new N();n.Foo(); // calls N's Foo((D)n).Foo(); // calls D's Foo((B)n).Foo(); // calls D's Foo</pre><p><b>Versioning Virtual Function Members</b><p><pre>class B { // written by the library people virtual void Foo() {...} // added in latest update}class D : B { // written by you void Foo() {...}}</pre><p><h4><u>Access Modifiers</u></h4><p><pre>// Assembly1.dllusing System;public class A { private int x=5; public void Foo() {Console.WriteLine (x);} protected static void Goo() {} protected internal class NestedType {}}internal class B { private void Hoo () { A a1 = new A (); // ok Console.WriteLine(a1.x); // error, A.x is private A.NestedType n; // ok, A.NestedType is internal A.Goo(); // error, A's Goo is protected }}// Assembly2.exe (references Assembly1.dll)using System;class C : A { // C defaults to internal static void Main() { // Main defaults to private A a1 = new A(); // ok a1.Foo(); // ok C.Goo(); // ok, inherits A's protected static member new A.NestedType(); // ok, A.NestedType is protected new B(); // error, Assembly 1's B is internal Console.WriteLine(x); // error, A's x is private }}</pre><p><b>Restrictions on Access Modifiers</b><p><h4><u>Classes and Structs</u></h4><p><b>Instance Members and Static Members</b><p><pre>class Panda { string name; static string speciesName = "Ailuropoda melanoleuca"; // Initializes Panda(See Instance Constructors) public Panda(string name) { this.name = name; } public void PrintName() { Console.WriteLine(name); } public static void PrintSpeciesName() { Console.WriteLine(speciesName); }}class Test { static void Main() { Panda.PrintSpeciesName(); // invoke static method Panda p = new Panda("Petey"); p.PrintName(); // invoke instance method }}</pre><p><b>Fields</b><p><pre>class MyClass { int x; float y = 1, z = 2; static readonly int MaxSize = 10; ...}</pre><p><b>Constants </b><p><pre>public static double Circumference(double radius) { return 2 * Math.PI * radius;}public static double Circumference(double radius) { return 6.2831853071795862 * radius;}</pre><p><b>Properties</b><p><pre>public class Well { decimal dollars; // private field public int Cents { get { return(int)(dollars * 100); } set { // value is an implicit variable in a set if (value>=0) // typical validation code dollars = (decimal)value/100; } }}class Test { static void Main() { Well w = new Well(); w.Cents = 25; // set int x = w.Cents; // get w.Cents += 10; // get and set(throw a dime in the well) }}</pre><p><b>Indexers</b><p><pre>public class ScoreList { int[] scores = new int [5]; // indexer public int this[int index] { get { return scores[index]; } set { if(value >= 0 && value <= 10) scores[index] = value; } } // property (read-only) public int Average { get { int sum = 0; foreach(int score in scores) sum += score; return sum / scores.Length; } }}class IndexerTest { static void Main() { ScoreList sl = new ScoreList(); sl[0] = 9; sl[1] = 8; sl[2] = 7; sl[3] = sl[4] = sl[1]; System.Console.WriteLine(sl.Average); }}</pre><p><b>Methods</b><p><b>Passing Arguments By Value</b><p><pre>static void Foo(int p) {++p;}static void Main() { int x = 8; Foo(x); // make a copy of the value-type x Console.WriteLine(x); // x will still be 8}</pre><p><b>Ref Modifier</b><p><pre>static void Foo(ref int p) {++p;}static void Test() { int x = 8; Foo(ref x); // send reference of x to Foo Console.WriteLine(x); // x is now 9}</pre><p><b>Out Modifier</b><p><pre>using System;class Test { static void Split(string name, out string firstNames, out string lastName) { int i = name.LastIndexOf(' '); firstNames = name.Substring(0, i); lastName = name.Substring(i+1); } static void Main() { string a, b; Split("Nuno Bettencourt", out a, out b); Console.WriteLine("FirstName:{0}, LastName:{1}", a, b); }}</pre><p><b>Params Modifier</b><p><pre>using System;class Test { static int Add(params int[] iarr) { int sum = 0; foreach(int i in iarr) sum += i; return sum; } static void Main() { int i = Add(1, 2, 3, 4); Console.WriteLine(i); // 10 }}</pre><p><b>Overloading Methods</b><p><pre>void Foo(int x);viod Foo(double x);void Foo(int x, float y);void Foo(float x, int y);void Foo(ref int x);void Foo(out int x);void Foo(int x);float Foo(int x); // compile errorvoid Goo (int[] x);void Goo (params int[] x); // compile error</pre><p><b>Operators</b><p><b>Implementing Value Equality</b><p><pre>class Note { int value; public Note(int semitonesFromA) { value = semitonesFromA; } public static bool operator ==(Note x, Note y) { return x.value == y.value; } public static bool operator !=(Note x, Note y) { return x.value != y.value; } public override bool Equals(object o) { if(!(o is Note)) return false; return this ==(Note)o; }}Note a = new Note(4);Note b = new Note(4);Object c = a;Object d = b;// To compare a and b by referenceConsole.WriteLine((object)a ==(object)b; // false //To compare a and b by value:Console.WriteLine(a == b); // true//To compare c and d by reference:Console.WriteLine(c == d); // false//To compare c and d by value:Console.WriteLine(c.Equals(d)); // true</pre><p><b>Custom Implicit and Explicit Conversions</b><p><pre>...// Convert to hertzpublic static implicit operator double(Note x) { return 440*Math.Pow(2,(double)x.value/12);}// Convert from hertz(only accurate to nearest semitone)public static explicit operator Note(double x) { return new Note((int)(0.5+12*(Math.Log(x/440)/Math.Log(2))));}...Note n =(Note)554.37; // explicit conversiondouble x = n; // implicit conversion</pre><p><b>Three-State Logic Operators</b><p><pre>public struct SQLBoolean ... { ... public static bool operator true(SQLBoolean x) { return x.value == 1; } public static bool operator false(SQLBoolean x) { return x.value == -1; } public static SQLBoolean operator !(SQLBoolean x) { return new SQLBoolean(- x.value); } public bool IsNull { get { return value == 0;} } ...}class Test { void Foo(SQLBoolean a) { if (a) Console.WriteLine("True"); else if (! a) Console.WriteLine("False"); else Console.WriteLine("Null"); }}</pre><p><b>Instance Constructors</b><p><pre>class MyClass { public MyClass() { // initialization code }}class MyClass { public int x; public MyClass() : this(5) {} public MyClass(int v) { x = v; }}MyClass m1 = new MyClass();MyClass m2 = new MyClass(10);Console.WriteLine(m1.x) // 5Console.Writeline(m2.x) // 10;</pre><p><b>Calling Base Class Constructors</b><p><pre>class B { public int x ; public B(int a) { x = a; } public B(int a, int b) { x = a * b; } // Notice how all of B's constructors need parameters}class D : B { public D() : this(7) {} // call an overloaded constructor public D(int a) : base(a) {} // call a base class constructor}</pre><p><b>Field Initialization Order</b><p><pre>class MyClass { int x = 5;}</pre><p><b>Static Constructors</b><p><pre>class Test { static Test() { Console.WriteLine("Test Initialized"); }}</pre><p><b>Static Field Initialization Order</b><p><pre>class Test { public static int x = 5; public static void Foo() {} static Test() { Console.WriteLine("Test Initialized"); }}</pre><p><b>Non-Determinism Of Static Constructor Calls</b><p><pre>class Test2 { public static void Foo() {} static Test() { Console.WriteLine("Test2 Initialized"); }}Test.Foo();Test2.Foo();</pre><p><b>Self Referencing</b><p><b>this Keyword</b><p><pre>class Dude { string name; public Test(string name) { this.name = name; } public void Introduce(Dude a) { if (a!=this) Console.WriteLine("Hello, I'm "+name); }}</pre><p><b>Base Keyword</b><p><pre>class Hermit : Dude { public void new Introduce(Dude a) { base.TalkTo(a); Console.WriteLine("Nice Talking To You"); }}</pre><p><b>Destructors and Finalizers</b><p><pre>protected override void Finalize() { ... base.Finalize();}</pre><p><b>Nested Types</b><p><pre>using System;class A { int x = 3; // private member protected internal class Nested {// choose any access-level public void Foo () { A a = new A (); Console.WriteLine (a.x); //can access A's private members } }}class B { static void Main () { A.Nested n = new A.Nested (); // Nested is scoped to A n.Foo (); }}// an example of using "new" on a type declarationclass C : A { new public class Nested {} // hide inherited type member}</pre><p><h4><u>Interfaces</u></h4><p><b>Defining an Interface</b><p><pre>public interface IDelete { void Delete();}</pre><p><b>Implementing an Interface</b><p><pre>public class TextBox : IDelete { public void Delete() {...}}public class TreeView : IDelete { public void Delete() {...}}public class TextBox : Control, IDelete {...}public class TreeView : Control, IDelete {...}</pre><p><b>Using an Interface</b><p><pre>class MyForm { ... void DeleteClick() { if (ActiveControl is IDelete) { IDelete d = (IDelete)ActiveControl; d.Delete(); } }}</pre><p><b>Extending an Interface</b><p><pre>ISuperDelete : IDelete {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -