2.c#1 Language Core Foundation

Source: Internet
Author: User

C#1 Language Core Foundation

This section focuses on several core foundations in the C#1 language, including: delegate, type System, value/reference type

1. Delegation

A delegate is similar to a function pointer, a series of actions to be performed at the appropriate time, and a delegate can be used if the code wants to perform an operation but does not know the details of the operation.

the composition of a simple delegate

There are 4 conditions that must be met to execute a delegate

    • Declaring a delegate type
    • Create a method that contains the code to execute
    • Create a delegate instance
    • Invoking a delegate instance

merging and deleting delegates

The above example delegate instance is only an operation, but the real situation is slightly more complicated, the delegate instance actually has an action list associated with it, this is the invocation list.

Static methods for the System.Delegate type combine and remove are responsible for creating a new delegate instance, combine is responsible for connecting the invocation list of two delegate instances together, and remove is responsible for removing the invocation list of another instance from one delegate instance.

Calls are rarely shown in C # code, combine generally use the + or + = operators, and remove generally uses-or =-operators.

All operations are executed sequentially when the delegate instance is invoked, and the return value of the Inovke is the return value of the last value if the delegate's signature has a non-void return value. If any of the methods in the invocation list throw an exception, subsequent operations are prevented.

Events

Events and delegates are very different directly, and events are not fields of the delegate type. Confusion arises because C # provides a shorthand way to allow the use of field-style events (syntax sugar provided by the C # compiler)

event< delegate type > event name

{

add{ // registering a function with its own defined delegate instance }

remove{ // de-Registering a function for its own defined delegate instance }

}

Summary
    • A delegate encapsulates a behavior that contains a special return type and a set of parameters, similar to an interface that contains a single method;
    • The type signature described in the delegate type declaration determines which method can be used to create the delegate instance while determining the signature of the call;
    • In order to create a delegate instance, a method is required and the target of the method is called;
    • The delegate instance is not easy to change;
    • Each delegate instance contains a list of calls (an action list)
    • Delegate instances can be merged together, or another can be removed from one delegate instance;
    • The event is not a delegate instance, but a paired add/reamove method
2. Characteristics of the type systemtype System for C #

The type system of the c#1 is static, explicit, and secure.

    • static types and dynamic types

C # is a static type: Each variable has a specific type, and the type is known at compile time. Only operations that are known to be of type are allowed, which is a compiler force.

Dynamic types can have many forms, and the nature of dynamic types is that variables contain values, but those values do not qualify a particular type, so the compiler cannot perform the same checks. C#4 introduces dynamic types, but most c#4 applications are still static classes

    • The display type is similar to an implicit

The difference between a display type and an implicit type is only meaningful in a statically typed language.

Display Type the type of each variable must display the specified string s= "Hello" in the declaration;

An implicit type allows the compiler to infer the type of the variable (var type inference) var s= "Hello" based on the purpose of the variable

    • Type safety and type insecurity

Type safety: C # language, CLR coercion type cast, incorrect throws exception, no direct manipulation of memory, etc.

Type unsafe: C and C + + languages, allowing direct data conversion, manipulation of memory, device pointers, etc. Powerful but error-prone

Summary
    • C#1 is static, displayed, and safe.
    • Static types do not allow a collection to be a strongly typed "string list" or "List of integers" unless a large number of duplicate codes are used for different elements
    • Method overrides and interface implementations do not allow covariance/contravariance
3. Value types and reference types

LINQ is the core of c#3, and LINQ is about querying, with the goal of using consistent syntax and features to make querying multiple data sources simple in an easy-to-read, composable way.

value types and reference types

Value type: Like only one newspaper, in order to give a friend, need to photocopy the entire contents of the newspaper and to him, then he has a complete and not related to the original newspaper.

Reference type: Similar to reading a Web page, compared to the newspaper you just need to send a URL address a friend, two people read the same content, the URL instead of the reference.

Most of. Net is reference types, where common reference types are: class, array, enumeration, delegate, interface, common value type int double float bool struct, etc.

misunderstanding
    1. Structs are lightweight classes
    2. Reference types are saved on the heap, and value types are stored on the stack

      Instance data of a reference type is saved in the heap, but the value type is not necessarily stored on the stack, and if there is a value type in a class, the value type should also be created on the heap

    3. Objects are passed by reference by default in C #.

      The value of a reference type variable is a reference, not the object itself. You can change the object content referenced by a parameter without having to pass the parameter itself by reference. When a reference type is used as a method parameter, the parameter is passed by default in a "value-passing" manner, but the value itself is a reference.

Packing and unpacking

Boxing: Converting a value type to a reference type

Unboxing: Converting a reference type to a value type

Summary
    • For an expression of a reference type, its value is a reference, not an object, referring to a similar URL, allowing you to access a small piece of data for real information
    • For an expression of a value type, his value is the actual data
    • Sometimes a value type is more efficient than a reference type, sometimes exactly the opposite
    • Objects of reference types are always on the heap, and values of value types can be either on the stack or on the heap

4. New features based on the C#1 Foundation

properties related to delegates

New features in c#2 related to delegates:

Generics (generic delegate type)

Expression used when creating a delegate instance

Anonymous methods

Delegated covariance/inverse degeneration

New features in c#3 related to delegates:

Lambda expression

characteristics related to type system

Features related to system types in c#2

Generic type

Limited delegated covariance/contravariance

Features related to system types in c#3

Anonymous type

Implicitly-typed

Extension methods

Features related to system types in c#4

Limited generic covariance/contravariance

Dynamic type

properties related to value types

Attributes related to value types in c#2

Generic type

Nullable type (nullable type)

2.c#1 Language Core base

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.