The core foundation of the C#1

Source: Internet
Author: User

One, commissioned

    • A delegate encapsulates a behavior that contains a special return type and a set of parameters, similar to containing a single method interface.
    • The type signature described in the delegate type declaration determines which method can be used to create the delegate instance, and also determines the signature of the call: the delegate type is really just a list of parameter types and a return type
    • Creating a delegate instance requires a method that has already called the target of the method:
void Invoke (String input) has a delegate of the same type and the same parameters
    • The delegate instance is not easy to deform: The delegate is not easy to change. The delegate instance is the same as string. Delegate.combine and String.Concat are like, all merging existing instances to form a new instance
    • Each delegate instance contains a list of calls-an action list:
The delegate instance also has an invocation list associated with it, such as:
    • Delegate instances can be merged together, and another can be removed from the delegate instance
    • An event is not a delegate instance, but a paired Add/remove method (a value and an assignment of a similar property)
delegate void StringProcessor(string input);//委托签名 class Person { string name; public Person(string name) { this.name = name; }   public void Say(string msg) { Console.WriteLine("{0}agy:{1}", name, msg); } public void tu(string msg) { Console.WriteLine("{0}agy:{1}", name, msg); } } #region 委托事件 Person jon = new Person("Jon"); Person tom = new Person("tom"); StringProcessor jons, toms, background;///创建委托实例 jons = new StringProcessor(jon.Say);//委托实例使用的方法 toms = new StringProcessor(tom.Say); background = new StringProcessor(Backgroud.Note); jons += toms; jons("Hello,jon"); toms("Hello,tom"); background("note"); #endregion two, type system features
    • C#1 is statically typed-the compiler knows which members you can use: Each variable has a special type, and the type is known at compile time and only operations known to that type are allowed.
    • C#1 is displayed--you must tell the compiler what type of variable it is: the type of each variable must be specified at the time of declaration.
    • C#1 is safe-you cannot use one type as another unless there is a real conversion relationship. (forced type conversions between completely unrelated structures, which can easily cause serious consequences)
    • Static types do not allow a collection to be a strongly typed "string list" or "List of integers" unless a large number of duplicate code is used for different elements: arrays are strongly typed, so it is not possible to set an element of a string[] to a FileStream, a reference type array that supports covariance, Conversions are allowed as long as the elements are between types.
    • The overrides and implementations of the method do not allow covariance/contravariance.

covariant refers to the ability to use a more derived type than the original specified derived type.

" contravariance " refers to the ability to use less derived types.

Just a little bit remember that Dog inherits from Animal,

so DogAnimal < Span data-wiz-span= "Data-wiz-span" > is the change of Harmony ( covariant ), and if animal  Dog ( invert )

Three, value types and reference types

    • For an expression (variable) of a reference type, its value is a reference, not an object:
    • A reference is like a small piece of data that url--allows you to access real data.
    • For a value-type expression, its value is the actual data.
    • The object of a reference type is always on the heap, and the value of the value type can be either on the stack or on the heap, depending on the context: the value of the variable is stored at its declared location, and there is an instance variable of type int in a class, then the value of this int is the same as the other data in the object, that is,
    • The value of a reference type is used as a method parameter, and the parameter is passed by default as "value"--but is worth itself as a reference.
    • Values of a value type are boxed when the behavior of a reference type is required, and vice versa: When a value type calls the Tostring,equals or GetHashCode method, boxing can also occur if the type does not overwrite these methods. Minimize the use of unpacking and boxing, which can affect performance when a large number of operations occur.
four, dynamic type

dynamic d = "hello";//动态类型 Console.WriteLine(d.Length); d = new string[] { "hi", "there" }; Console.WriteLine(d.Length);

The core foundation of the C#1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.