Should I learn C net

Overview of C #

  • 6 minutes to read

C # (pronounced “C Sharp”) is a modern, object-oriented and type-safe programming language. C # enables developers to create a wide variety of secure and robust applications that run in the .NET ecosystem. C # has its roots in the C language family and is instantly familiar to programmers working with C, C ++, Java, and JavaScript. This introduction provides an overview of the key components of the language in C # 8 and earlier versions. If you want to learn about the language with interactive examples, work through the tutorials on the Introduction to C # page.

C # is an object-oriented, component-oriented Programming language. C # provides language constructs to directly support these concepts, making C # a natural language in which software components are created and used. Since its release, C # features have been added to support new workloads and methods of designing software.

Several C # features help create stable and durable applications. The *Garbage collection _ automatically releases main memory that is occupied by unreachable, unused objects. Nullable types provide protection against variables that do not refer to assigned objects. The Exception handling provides a structured and extensible approach to failure detection and recovery. Lambda expressions support functional programming techniques. The Language Integrated Query Syntax (LINQ) creates a common pattern for working with data from any source. Thanks to language support for asynchronous operations a syntax for the construction of distributed systems is provided. C # provides a _ *uniform type system**. All C # types, including primitive types like and, inherit from a single root type. All types share a number of general processes. Values ​​of any kind can be stored, transported and processed in a standardized manner. In addition, C # supports custom reference and value types. C # enables dynamic allocation of objects and inline storage of lean structures. C # supports generic methods and types that provide better type safety and performance. C # provides iterators that collection class implementers can use to define custom behaviors for client code.

In C #, the Version management to ensure that programs and libraries can continue to develop in a compatible manner over time. Aspects of C # development that were directly influenced by version control considerations include the separate and modifiers, the rules for overload resolution, and support for explicit interface member declarations.

.NET architecture

C # programs run on top of .NET, a virtual execution system called Common Language Runtime (CLR), and class libraries. The CLR is the implementation of the Common Language Infrastructure (CLI) from Microsoft, an international standard. The CLI is the foundation for creating execution and development environments in which languages ​​and libraries work together seamlessly.

The source code, written in C #, is compiled into an intermediate language that conforms to the CLI specification. The IL code is used together with resources such as B. Bitmaps and strings are stored in an assembly, usually with the extension .dll having. An assembly contains a manifest that provides information about the type, version, and culture of the assembly.

When the C # program runs, the assembly is loaded into the CLR. The CLR converts the IL code into native computer instructions using just-in-time (JIT) compilation. The CLR provides additional services for automatic garbage collection, exception handling, and resource management. The code executed through the CLR is sometimes referred to as "managed code". This is in contrast to “unmanaged code”, which is compiled in native machine language that is tailored to a specific platform.

One of the most important features in .NET is language interoperability. The IL code generated by the C # compiler corresponds to the general type system (CTS, Common Type Specification). The IL code generated through C # can interact with code generated through the .NET versions of F #, Visual Basic, C ++, or any of the more than 20 other CTS-compliant languages. A single assembly can contain multiple modules written in different .NET languages, and the types can refer to each other as if they were written in the same language.

In addition to the runtime services, .NET also contains extensive libraries. These libraries support a wide variety of workloads. These are organized in namespaces that provide a variety of useful functions: from file input and output, to string manipulation, to XML parsing, and to web application frameworks and Windows Forms controls. A typical C # application makes extensive use of the .NET class library to perform routine operations.

For more information on .NET, see the .NET overview.

Hello World

The "Hello, World" program is commonly used to introduce a programming language. Here it is in C #:

The Hello, World program starts with a policy that references the namespace. Namespaces provide a hierarchical way to organize C # programs and libraries. Namespaces contain types and other namespaces. For example, the namespace contains a number of types, such as the class referenced in the program, and a number of other namespaces, such as and. A policy that references a specific namespace allows unqualified use of the types that are members of that namespace. Because of the directive, the program can be used as an abbreviation for.

The class declared by the Hello, World program has a single member: the method. The method is declared with the modifier. Although instance methods can refer to a specific enclosing object instance using the keyword, static methods act without reference to a specific object. By convention, a static method called acts as the entry point of a C # program.

The output of the program is generated using the method of the class in the namespace. This class is provided using the standard class libraries, which are automatically referenced by the compiler by default.

Types and Variables

There are two types of types in C #: Value types and Reference types. Value type variables contain their actual data. Reference type variables, on the other hand, store references to their data - the latter are known as objects. Thanks to reference types, two variables can refer to the same object. For example, operations applied to one variable can affect the object referenced by the other variable. In the case of value types, the variables each have their own copies of the data. Operations applied to one variable cannot affect the other variable (except in the case of the parameter variables and).

A Identifier is a variable name. An identifier is a sequence of Unicode characters with no spaces. An identifier can be a C # reserved word if it is preceded by the prefix. Using a reserved word as an identifier can be useful when interacting with other languages.

C # value types are further broken down into simple types, Enumeration types, Structure types, Nullable value types and Tuple value types. C # reference types are further broken down into Class types, Interface types, Array types and Delegate types.

Below is an overview of the C # type system.

  • Value types
    • Simple types
      • Signed integral:,,,
      • Unsigned integral:,,,
      • Unicode Character: representing a character as a UTF-16 code unit
      • Binary floating point (IEEE):,
      • High precision decimal floating point number:
      • Boolean: used to represent Boolean values ​​that are either or
    • Enumeration types
      • Custom types of shape. A type is a stand-alone type with a named constant. Each type has an underlying type, which must be one of eight integer types. The set of values ​​of a type is identical to the set of values ​​of the underlying type.
    • Structure types
      • Custom types of shape
    • Value types that can be set to NULL
      • Extensions of all other value types with a value
    • Tuple value types
      • Custom types of shape
  • Reference types
    • Class types
      • Ultimate base class of all other types:
      • Unicode Strings: representing a sequence of UTF-16 code units
      • Custom types of shape
    • Interface types
      • Custom types of shape
    • Array types
      • One-dimensional arrays, multidimensional arrays, and jagged arrays, for example, and
    • Delegate types
      • Custom types of shape

Use C # programs Type declarationsto create new types. A type declaration specifies the name and members of the new type. Six type categories in C # can be user-defined: class types, structure types, interface types, enumeration types, delegate types, and tuple value types.

  • A type defines a data structure that contains data members (fields) and function members (methods, properties, and so on). Class types support single inheritance and polymorphism. These are mechanisms by which derived classes can be extended and base classes can be specialized.
  • A type is similar to a class type in that it is a structure of data members and function members. Unlike classes, structures are value types that typically do not require heap allocation. Structure types do not support user-defined inheritance, and all structure types implicitly inherit from type.
  • A type defines a contract as a named group of public members. An or type implementing a type must provide implementations of the members of the interface. One can inherit from multiple base interfaces and one or more interfaces implement.
  • A type represents references to methods with a specific parameter list and return type. Delegates allow methods to be treated as entities that can be assigned to variables and passed as parameters. Delegates are provided analogously to function types of functional languages. They are also conceptually similar to function pointers found in some other languages. In contrast to function pointers, delegates are object-oriented and type-safe.

The types,,, and support generics, which means that they can be parameterized with other types.

C # supports one- and multi-dimensional arrays of all types. Unlike the types listed above, array types do not need to be declared before they can be used. Instead, array types are created by putting square brackets after a type name. For example, a one-dimensional array is off, a two-dimensional array is off, while a one-dimensional array of one-dimensional arrays or a jagged array is off.

Nullable types do not require a separate definition. For each non-nullable type there is a corresponding nullable type that can contain an additional value,,. For example, is one type that can contain any 32-bit integer value or value. is a type that can contain any -type or the value.

The C # type system is unified in that a value of any type can be treated as. Every type in C # is directly or indirectly derived from the class type, and is the ultimate base class of all types. Reference type values ​​are treated as objects by simply displaying the values ​​as a type. Values ​​of value types are obtained by executing Boxing- and Unboxing operations treated as objects. The following example converts a value to and then back to a value.

When a value of a value type is assigned to a reference, a “box” is assigned to contain the value. This box is an instance of a reference type and the value is copied into this box. Conversely, when a reference is converted to a value type, a check is made to ensure that the referenced type is a box of the correct value type. After a successful check, the value in the box is copied to the value type.

As a result of the uniform C # type system, value types are treated as references "when requested". Because of the unification, general purpose libraries that use type can be used with all types derived from, including both reference and value types.

There are several types of variables in C #, including fields, array elements, local variables, and parameters. Variables represent storage locations. Each variable is of a type, as shown below, which determines what values ​​can be stored in the variable.

  • Value type that cannot be set to NULL
    • A value of exactly this type
  • Value type that can be set to NULL
    • A value or a value of this exact type
  • object
    • A reference, a reference to an object of any reference type, or a reference to a nested value of any value type
  • Class type
    • A reference, a reference to an instance of this class type, or a reference to an instance of a class that is derived from this class type
  • Interface type
    • A reference, a reference to an instance of a class type that implements that type of interface, or a reference to a nested value of a value type that implements that type of interface
  • Array type
    • A reference, a reference to an instance of this type of array, or a reference to an instance of a compatible type of array
  • Delegate type
    • A reference or a reference to an instance of a compatible delegate type

Program structure

The most important organizational concepts in C # are *Programs _, Namespace, Types, Member and Assemblies Programs declare types that contain members and can be organized into namespaces. Classes, structures, and interfaces are examples of types. Fields, methods, properties, and events are examples of members. When C # programs are compiled, they are physically packaged in assemblies. Assemblies typically have the extension or, depending on whether they have Applications or _ *Libraries** to implement.

As a simple example, take an assembly that contains the following code:

The fully qualified name of this class is. The class contains several members: a field with the name, two methods with the name and, and a nested class with the name. The class contains three other members: a field with the name, a field with the name, and a constructor. is a generic Class. It has a type parameter, which is replaced by a concrete type when it is used.

Note

A stack is a FILO collection (First In, Last Out). New items are added at the top of the stack. An item is removed from the top of the stack.

Assemblies contain executable code in the form of Intermediate Language (IL) instructions and symbolic information in the form of metadata. Before execution, the Just-In-Time (JIT) compiler of the .NET common language runtime converts the IL code in an assembly into processor-specific code.

Because an assembly is a self-describing unit of code and metadata, there is no need for directives and header files in C #. The public types and members contained in a particular assembly are exposed simply by referencing the assembly when the program is compiled in a C # program. For example, this program uses the class from the assembly:

To compile this program, you need to access the assembly referthat contains the batch class defined in the previous example.

C # programs can be stored in multiple source files. When a C # program is compiled, all of the source files are processed together. The source files can refer to each other freely. Conceptually, it is as if all the source files had been concatenated into one large file before processing. Forward declarations are never required in C # because the order of the declaration is insignificant with a few exceptions. C # does not restrict a source file to the declaration of a single public type, nor does the name of the source file have to match a type declared in the source file.

Additional articles in this introduction explain these organizational blocks.