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 Runtime Library ), themono program can run on a variety of operating systems and game platforms (currently Mono is not mature enough to be less influential ).

The . NET principle is primarily the principle of compilation : IL Intermediate Language , CLR runtime environment.

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, called the underlying C + + code COM) to the compiler -defined Intermediate Language IL.

2) Runtime : The CLR(. NET or mono implementation) uses the IL language as JIT(on-demand, instant compilation) as a dedicated Machine code , the JIT compiled machine code stored, the next time you run this part of the code is not compiled.

So you need to install the. NET runtime (CLR suite of code) into a machine that needs to run a. NET program to run the program.

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

Flowchart during the compilation and execution of. NET programs:



Second, the advantages and principle of detail:

1. A powerful. Net Framework managed Code Collection class that encapsulates most of the technology component classes used on Windows, file systems, UI interfaces, data source access, network access, COM interop (graphics image Multimedia, WPF graphics system), None can be obtained by calling the Win API function from the. NET platform.

2. Simpler language features : Automatic memory management, single inheritance, support for events, delegates, Attributes, LINQ and a range of features that make business development easier.

3. Web application development is fast (. NET Framework support, control can be dragged (UI facilitates editing and positioning), add events (jump to Control Logic layer), ADO data source Access, XML Network class Library, Windows service).

Web form: 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 can support many of the Windows service first-off things.

Desktop app Development, window form: WPF in System.windows.forms.control,.net 3.0.

4. language Platform independent of the compilation mechanism, and faster running speed (MS not recommended , mono 's Xamarin, Unity is good ): Compiled into IL Intermediate Language ,CLR Common language runtime managed code , Depending on the runtime program, the CLR compiles the Il Intermediate language into internal machine code with JIT-on-the-fly compilation, which caches the compiled machine code and improves the program speed.

All managed languages in. NET use such a compilation mechanism, that JavaScript is compiled in. NET, not interpreted. Managed C + + can call 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 call managed code.

platform-Independent:Ms. NET with IL and CLR theory can be done but not actually cross-platform, Mono is a cross-platform. NET implementation but less influential.

Interaction between languages:

Com:com is based on binary communication, where the caller (client program) can instantiate the required COM object with the COM runtime, and then invoke the required method or property through a COM object pointer. It is limited (but more coupled) that COM does not allow client programs to implement COM classes, and COM can only be debugged separately for different languages, because of the limitations of communication between components and components only through the COM runtime (mutual instantiation).

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 contain instances of another language ( associative combinatorial aggregation ) , objects can be passed between methods ( dependent ) , can handle exceptions thrown by each other ( a unified . NET class is defined to represent exceptions ) , you can debug 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 benefits 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: A strong data type, which must have a variable or constant type, cannot use a variant, and the pointer can be used in a tagged c # (non-memory-safe) or Managed C + + (non-memory safe).

CTS (Common type System): All types defined by the managed language are compiled into a common type defined by the intermediate language; in VB, int in C # is compiled and mapped to int32, so that different languages can inherit each other through the CTS, correlate 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-descriptions), self-describing (arrays, classes), classes (user-defined classes, delegates, boxed value types).

CLS (Common Language Specification): CLS is a standard set of minimum language specifications, such as the CLS specifies not to use any names that are only case-insensitive (case-insensitive syntax), and the previous VB code can be used 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 required). NET is a garbage collector. When the. NET runtime CLR detects that the managed heap for a given process is full and needs to be cleaned up, it calls the garbage collector, checks all references to objects in the managed heap, and cleans up memory for objects referencing 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 large 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 free up memory resources.


code Security:window is a role-based security mechanism,. NET provides code-based security, because the intermediate language provides type security, the CLR checks the code before it runs, determines whether it has the required security permissions, and the CLR cannot execute the code without permissions.

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 and fast 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 to achieve communication ( Memory-sharing mechanism can also result in a loss in performance).

The. NET CLR introduces an application domain, and hopefully resolves the problem. There can be multiple application domains in a process space, and an application domain roughly corresponds to an application that executes a thread that corresponds to a specific application domain.

In this way, applications that require communication are theoretically added to an application domain in the same process, and applications in the process space can access each other's data directly, but there is a type-safety mechanism for intermediate languages (C # does not recommend using pointers, and the CLR guarantees security checks on array boundaries without crossing the bounds). Therefore, you cannot communicate unless you explicitly use unsafe features. However, you can use. NET remote services to quickly communicate or share data between applications that are secure or insecure.

For communication between different components, components can be partitioned into different application domains, and a large application consists of multiple application domains. This allows for fast 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, EXE runs the program architecture, and the assembly concept is used in. Net. The Assembly is in order to solve the previous COM component registration Trouble , theCOM component information gets troublesome (need to get the GUID and interface of the component from the registry, the 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 contains compiled, object-oriented,. NET CLR, describing all types in the assembly, and the details of the members of those types.

Address registration hassles with file sets (direct storage), troubleshoot component information with assembly manifests and metadata , and load assemblies with the assembly portal file to describe the details, hashes, and contents of other files. Avoid problems with files being replaced or stuffed, still loading assemblies, causing data to be out of sync.

An assembly can be stored in multiple files (dynamic assemblies are stored in memory), and if an assembly is stored in multiple files, there is a master file with an entry point that describes other file details, hashes, and content in the assembly, and if a file is replaced or stuffed, the system will definitely detect it. and refuses to load the assembly.

Assemblies are divided into: executable assemblies and library assemblies that use the same assembly structure, except that an executable assembly contains a main 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 can be used, and the. NET assembly is a private assembly by default, and the installation is very simple- just put the appropriate files in the corresponding folder in the file system (no registry key is required). Private assemblies cannot be used by other programs, because applications can only load assemblies that reside in the same folder or subfolders as the primary execution file.

Shared assemblies: Common libraries that other applications can also use, need to be able to implement fast sharing, need to prevent assembly naming conflicts, and overwrite different versions of an assembly.

Shared: Shared assemblies require special handling, need to be done with. NET tools, place shared assemblies in the global Assembly cache (GAC), establish a small folder hierarchy, and examine shared assemblies.

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

Version overrides: You can either specify the version information in the assembly manifest, or you can resolve it 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 useful in the Vendong manual and can be used in conjunction with reflection technology to perform programming tasks based on characteristics. Makes one language defined in another language that can be read.

Reflection: With the type and type member details described by the assembly, you can get the details of the attribute at run time, or as an indirect way of instantiating a class or invoking a method, if the class name on the method is specified as a string (the class name is a parameter), you may choose to instantiate the method so that it is called at run time. 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: Inappropriate to do time- performance (High-speed algorithm) or flexible space performance (memory immediate release) program, because the intermediate language and the compilation process, than the c/c++native type of language will be slower, Automatic memory recovery It is difficult to immediately release unwanted memory, without inline functions and destructors (not recommended).

Limited support beyond the 2.Windows platform: mono is not mature enough to use, window Java,python etc is very powerful and equally 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 Runtime Libraries, and application sets, the application domain is a great innovation for application compilation and application composition Systems , but the pros 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 is not a substitute for Java,oc,python,lua and other seniors or epigenetic. In short, the language is the tool only (master is more in person, not in the sword, in what occasion to take the most appropriate sword, or saber or spear).

Basic principles and advantages and disadvantages of C # language

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.