Basic principles and advantages and disadvantages of C # language

Source: Internet
Author: User

First, the principle:

C # is a language created specifically for the . NET program framework.

The. net Framework has Ms . Netframework;mono . NETFramework( also compliant with the . NET IL language,CTS specification,CLS specification , CLR execution-Time library ), themono program can execute on a variety of operating systems and game platforms (at the moment mono is not mature enough to be less influential ).

The . NET principle is mainly the principle of compiling : IL Intermediate language (CTS,CLS specification, language mutual invocation ), CLR Execution time Environment (. NET virtual onboard assembly compiled into machine language, Memory management (allocation and recycling), event framework mechanism, staging mechanism, application domain creation and component communication, code security check and exception handling .

The main concepts of . NET Program composition system: Application set, application domain.

The. NET program compilation process:

1) Compile time: the. Netvs or mono compiler (or other compiler) compiles the source code (C#,VB, managed c++,j#,jscript, and calls to the underlying C + + code COM) to the compiler Defines the intermediate language IL.

2) execution: The CLR(. NET or mono implementation) uses the IL language as a dedicated machine code on the platform with JIT(On Demand) The JIT-compiled machine code is stored, the next time you execute this part of the code is not compiled.

Therefore, it is necessary to install the. NET execution library (CLR suite of code) into the machine where the. NET program is required to execute the program.

The languages in. NET are compliant with the. NET specification, such as strong typing, single inheritance, semi-colon, and so on, the details of the language itself are not too demanding.

Flowchart for. NET programs during compilation and run:



Second, strengths and principles of detail:

1. A powerful. Net Framework managed Code Collection class that encapsulates most of the technology component classes used on Windows, file system, UI interface, data source access, network access, COM interop (graphics image Multimedia, WPF graphics system), There is no ability to invoke the win API function via the. NET platform.

2. Simpler language features : self-active memory management, single inheritance, support events, trusts, Attributes, LINQ and a series of features to make business development easier.

3. Web application development is fast (. NET Framework support, control can be dragged (UI easy to edit and locate), join events (jump to the control Logic layer), ADO data source Access, XML Network class library, Windows Services).

Web window: System.Web.UI.WebControls.WebControl class.

The XML Web service, the client request, returns XML instead of the HTML. NET page of the server.

Window Service: the. NET base class in the System.ServiceProcess namespace, capable of supporting very many window service first-off things.

Desktop Application Development, Window window: WPF in System.windows.forms.control,.net 3.0.

4. Language Platform-independent compilation mechanism, and faster execution speed (MS not recommended , mono 's Xamarin, Unity is good ): Compiled into IL Intermediate Language ,CLR Common Language Execution library managed code , The CLR compiles the Il Intermediate language into internal machine code according to the execution time program, and caches the compiled machine code to improve the program speed.

All managed languages in. NET use this compilation mechanism, that is, JavaScript is compiled in. NET, not interpreted. Managed C + + has the ability to invoke unmanaged C + +, but there are restrictions on C + + in. NET that cannot use multiple inheritance of templates or classes, and to enable pointers also requires that items specified as CLR memory types are non-secure. NET combines COM interoperability, so managed code can call COM components. COM components can also invoke managed code.

platform-agnostic:Ms. NET can do this with IL and CLR theory, but not across platforms, and Mono is a cross-platform. NET implementation with less impact.

Interaction between languages:

Com:com is based on binary communication, where the caller (client program) is able to instantiate the required COM object with a COM execution library and then invoke the required method or property through a COM object pointer. There is limited communication between components and components only through the COM execution Library (mutual instantiation), COM does not agree with client program inheritance to implement COM classes, COM can only be debugged separately for different languages.

il and CLR: the. NET compiler, compiled into uniform intermediate code (CLR specification), is a higher layer than binary, making interaction between languages more flexible.

classes in different languages can inherit ( inherited ) , a language can include an instance of another language ( associative combinatorial aggregation ) , objects can be passed between methods ( dependent ) , ability to handle exceptions thrown by each other ( a unified . NET class is defined to represent exceptions ) , can debug the source code written in different languages.

Il Intermediate Language Specification:

The main features of intermediate languages: object-oriented and use interfaces, huge differences between value types and reference types, strong data types, and the use of exception classes to uniformly handle exceptions in each language, using attributes.

The advantages of these specifications in intermediate languages:

language interoperability, garbage collection, security, application domain implementation .

Language interoperability:

Specification for data storage: Reference types are always stored in the memory area of the managed heap, and value types are generally stored in the stack.

Data type specification: Strong data type, must understand the type of a variable or constant, cannot use a variant, the pointer in the marked C # (non-memory security) or managed C + + (non-memory security) can be used.

CTS (Common type System): The types defined by all managed languages are compiled into common types defined by the intermediate language, and int in VB is compiled and mapped to int32, so that different languages can inherit each other through the CTS, correlate the combinations, and rely on the implementation of communication. Types are divided into value types and reference types, value types (built-in, enumerations, user-defined value types), references (interfaces, pointers, self-descriptive narratives), self-descriptive narratives (arrays, classes), classes (user-defined classes, entrusted, boxed value types).

CLS (Common Language Specification): CLS is a standard set of minimum language specifications, such as CLS specifies not to use names that are distinguished by uppercase and lowercase (without distinction between uppercase and lowercase syntax), where the once-VB code can be used in conjunction with CLS-Compliant code to customize compilation options ( Compilers that support part of the CLR feature are written, and communication between languages is communicated to each other.


garbage collector (GC): com is a reference count, AddRef count plus 1,release count minus 1 and up to 0 to free memory (or manual release memory).. NET is a garbage collector, When the. NET execution Library CLR detects that the managed heap of a given process is full and needs to be cleaned up, it calls the garbage collector, examines the references to objects in all the managed heaps, and cleans up memory for objects referenced as 0.

Note: Long-term holding in a reference ring can lead to memory footprint leaks, such as objects placed in a global or static container, object pools in a database or network link, and so on.

CLR garbage collector calls are not deterministic, so for program logic with a larger memory overhead, you need to manually invoke the garbage collector in your code.

A call destructor that can also be displayed in C # can easily release memory resources.


code Security:window is a role-based security mechanism, and. NET provides code-based security, because the intermediate language provides type safety, and the CLR checks the code before execution to determine if there is a need for security permissions, and the CLR cannot execute the code without permission.

5. Better program component structure (application domain and assembly):

An application domain is an area of memory in a process space that is used as an application domain for secure, high-speed communication between components and applications.

Assembly

application domain (in process space): The original window application component DLL is loaded in the same process (4GB of memory space and security identity, one component error can affect other components, resulting in a crash), or EXE between the inter-process replication data for communication ( Memory-sharing mechanism can also result in a loss in performance).

The. NET CLR introduces an application domain, which is expected to solve this problem. There can be multiple application domains within a process space, an application domain roughly corresponding to an application, and a detailed application domain running on a thread.

Thus, in theory, applications that need to communicate are added to an application domain in the same process, and applications in the process space can directly access each other's data, but there is a type-safety mechanism for intermediate languages (C # does not recommend using pointers, and the CLR ensures that the security checks on the array boundary are not crossed). Therefore, you cannot communicate unless you understand the use of unsafe features. However, through. NET Remote services, you can communicate or share data between applications that are secure or insecure with high speed.

For communication between different components, the components can be partitioned into different application domains, and a large application consists of multiple application domains. This allows for high-speed communication between components and does not cause one component to fail, which can affect the overall system crash.

Assembly (in disk component): The original is the. Lib,.dll library component, the EXE Executor program architecture, and the assembly concept used in. Net. The Assembly is in order to solve the ever com component of the trouble,com component information get trouble (need to get the GUID and interface of the component from the Register table, properties and methods also need to get from the class library), Possible component data information is out of sync (shared data is stuffed and DLLs are overwritten by other DLLs with the same name).

An assembly (assembly) is a code logical unit that includes a compiled, object-oriented,. NET CLR that describes all the types in the assembly, and the details of the members of those types.

Use file set to solve the issue of the manual (direct storage), the assembly manifest and metadata to solve the problem of component information, and loading the assembly with the assembly Portal File Description of the other file details, hashing, content, Avoid problems with files being replaced or stuffed, and still loading assemblies, causing the data to be out of sync.

An assembly can be stored in multiple files (the dynamic assembly is stored in memory), assuming that an assembly is stored in multiple files, including a master file that includes the entry point, which describes the details, hashes, and contents of other files in the assembly, assuming that a file is replaced or stuffed, and the system is sure to detect it. and refuses to load the assembly.

Assemblies are divided into: can run assemblies and library assemblies , they use the same assembly structure, and only the assemblies that can be run include a main program entry point. It is also possible for programs to run into static assemblies, dynamic assemblies, static assemblies stored in disk files, and dynamic assemblies stored in memory. Types are divided into: shared assemblies and private assemblies .

Private assemblies: Only a single application is available, the. NET assembly is a private assembly by default, and easy to install, simply by placing the appropriate files in the appropriate directory in the file system (without the need to register entries). A private assembly cannot be used by another program, because an application can only load an assembly in the same directory or its subdirectories as the main running file.

Shared assemblies: Common libraries that other applications can use also require high-speed sharing, the need to prevent assembly naming conflicts, and the overlap between the different version numbers of the assemblies.

Shared: Shared assemblies require special processing, complete with. NET tools, place shared assemblies in the global assembly Fast Cache (GAC), establish a small directory hierarchy, and examine shared assemblies.

Naming conflicts: Shared assemblies should specify a name based on private key cryptography (a private assembly needs to specify just the same name as the primary file name), which must be referenced by the program that references the shared assembly.

Version number overrides: The ability to specify the version number information in the assembly manifest can be resolved by installing it at the same time.


Other:

Features: Provides a mechanism for the compiler to use. NET to provide information about certain items in the program. It is very useful in document specification and can be used in conjunction with reflection technology to run programming tasks according to characteristics. Makes it possible to read in a language that is defined in a language.

Reflection: The description of the description of the type of the assembly and the type of member specific information, you can obtain specific information about the characteristics during the execution period, or as an indirect way of instantiating a class, invoking a method, assuming that the class name on the method is specified as a string (class name is a parameter), you can select the class to instantiate the method, Instead of being called at compile time.

64bit support, generics and iterators, anonymous methods, partial classes, nullable types.


Third, disadvantages:

1. low-level and high-performance inappropriate: A program that is not suitable to do time- efficient (fast algorithm) or very flexible space performance (memory is released immediately), because of intermediate language and compilation process, than c/c++native type of language will be slower , the memory of the active recovery is difficult to release the unnecessary memory immediately, do not use the inline function and destructor (not recommended).

Limited support beyond the 2.Windows platform: mono is not mature enough to use, window Java,python etc is very strong and the same excellent.


Iv. Summary:

C # is more like a glue and a powerful application-layer language (bottom-level is C/S, WINAPI, COM Ole VB, DirectX, or a custom library on a mono project), and . NET -based IL CLR Execution-time libraries, and application sets, the application domain is an innovation that is much larger than the application-compilation and application-composition system , but the strengths at the same time also bring drawbacks Low-level or real-time high-performance applications that are difficult to replace c/c++,windows platforms other than the development of applications also need to continue to progress (Xamarin, Unity is doing well), it can not replace the Java,oc,python,lua and other seniors or epigenetic. In short, language is a weapon.

Basic principles and advantages and disadvantages of C # language

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.