Easily learn the structure and classes of C

  • 2021-08-21 21:11:07
  • OfStack

Classes and constructs are the two basic constructs of the same type system in. NET Framework. Both belong to data structures in essence, encapsulating the data and behavior of this group as a logical unit. Data and behavior are "members" of a class or structure that contain their own methods, properties, events, and so on.
Structure
Structures are the most common mechanism used by C # programmers to define their own value types. Structures are more powerful than enumerations because they provide methods, fields, operators, access control, and so on.
Structures are similar to classes in that they represent data structures that can contain data members and function members. However, unlike classes, structures are of one value type and do not require heap allocation. A variable of structure type directly contains the data of that structure, while a variable of class type contains only one reference to the corresponding data (the referenced data is called "object").
Structure is especially useful for small data structures with value semantics. Complex numbers, points in coordinate systems, or "key-value" pairs in dictionaries are typical examples of structures. The key to these data structures is that they are only a small number of data members, do not require inheritance or reference identifiers, and are easy to use (copy the value directly instead of copying its reference when assigning values).
The declaration of the structure is implemented by the keyword struct, and the declaration format is:
Modifier struct structure name
{
Main body of structure
};
The structure declaration contains an optional set of attributes, followed by an optional set of structure modifiers, followed by the keyword struct and an identifier for naming the structure, followed by an optional structure interface specification, and finally a structure body, which can be followed by a semicolon as needed.
The structure declaration can contain one structure modifier as needed: new, public, protected, internal, private
Use of structure
It is an error to define a default (no argument) constructor for a structure, and it is also an error to initialize an instance field in a structure. Initializing structure members can be done in two ways: 1 by using a parameterized constructor, and 2 by accessing the members separately after the structure is declared. Any private or otherwise made inaccessible member can only be initialized in the constructor.
If you create a structured object using the new operator, the structured object is created and the appropriate constructor is called. Unlike classes, structures can be instantiated without the new operator. In this case, there is no call to the constructor, so the allocation efficiency can be improved. However, until all fields are initialized, the fields remain unassigned and the objects are unavailable.
When a structure contains a reference type as a member, the default constructor that calls that member must be explicitly invoked, otherwise the member remains unassigned and the structure is unavailable.
Example, create a structure and analyze the use of the structure


<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace Text 
{ 
 struct Sum// Defined 1 A structure  
 { 
  public int sum(int a, int b)// Methods contained in the structure  
  { 
   return a + b; 
  } 
 } 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   int p = 10; 
   int q = 10; 
   Sum t = new Sum();// Create a structure object t 
   Console.Write("{0}+{1}=",p,q); 
   Console.Write(t.sum(p,q));// Invoke the method in the structure  
   Console.ReadLine(); 
  } 
 } 
} 
</span> 

The output result is: 10+10=20
Class
A class is a data structure that encapsulates data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, static constructors, and destructors), and other classes (nested types). Class is the template for creating objects. The 1-cut types of C # are all classes, all statements must be within the class, and there are no statements outside the class. Therefore, classes are the core and basic building blocks of C # language. Class types support inheritance, which is a mechanism that enables derived classes to extend and specialize their base classes.
The base class specified in the class declaration can be a constructed class type. The base class itself cannot be a type parameter, but it can contain type parameters in its scope.
In OOP method, class is a highly abstract and generalization of the real world, while object is an instance of class. Object 1 must have the common characteristics and behavior rules of its generic class, and of course an object can also have the characteristics and behavior rules not specified by its generic class. This point is the same as real life, and such simulation and abstraction are more in line with people's thinking habits, which is the basic reason why OOP method has strong vitality and can be welcomed by more and more software workers and supported by many computer developers.
In a word, from the programmer's point of view, class is a data pattern and several program processes, which are encapsulated and formed as a whole, and it is a kind of simulation and abstraction of the real world by using information technology. The object is an instance of a class. From the programming language, the object can be understood as the result of a class assignment. Object is a component of OOP method.
The declaration of a class in C # is via an instance of the class keyword in the format:
Modifier class class name: base class or interface
{
Class body
}
Where "modifier" and ": base class or interface" are optional. A class modifier can be one of the following or a combination of them (the same one modifier is not allowed to appear more than once in the class declaration ()).
(1) new: Allowed only for nested class declarations, indicating that members inherited from the base class with the same name are hidden from the class
(2) public: Indicates that access to this class is not restricted
(3) internal: Only its class can access it
(4) private: Access only to applications or libraries in. NET
(5) abstract: Abstract class. Instances of class are not allowed
(6) sealed: Sealed class, not allowed to be inherited
Use the new keyword to create one instance of a class:


<span style="font-size:18px;"> class A 
 { 
 } 
 class B 
 { 
  void M() 
  { 
   A a = new A(); 
  } 
 }</span> 
   

Class inheritance declaration: Only single inheritance is supported in C #


<span style="font-size:18px;"> class A 
 { 
 } 
 class B : A 
 { 
 }</span>

(1) Constructors and destructors
C # provides a better mechanism to enhance the security of programs. The C # compiler has strict type safety checking, which can find almost all syntax problems in programs. However, just because the program passes the compile check does not mean that the error no longer exists.
C # language fully considers the occurrence of program errors and solves them well, that is, it puts the initialization of objects in the constructor and the clearing work in the destructor. When the object is created, the constructor is executed automatically. When the object dies, the destructor executes automatically.
The name of the constructor can't be named casually, and it must be recognized by the compiler before it can be automatically executed. Its naming method is simple and reasonable: make the constructor the same as the class name. In addition to the name, another special feature of the constructor is that it has no return value type, which is different from functions with a return value type of void.
A destructor is a method member that destroys an instance of a class. Destructors cannot have arguments, can't have any modifiers, and can't be called. The purpose of destructor is different from that of constructor, so prefix "~" is added before destructor to show the difference.
Constructor and destructor are formally simpler functions in a class. However, their use is not as simple as it seems, so using constructors and destructors flexibly and correctly can help users better understand the memory management mechanism of CLR and better manage the resources in the system.
(2) Classes and Objects
Class is a collection of 1 set of objects with the same properties and services. It provides an abstract description of all objects belonging to this class, and its interior includes two main parts: attributes and services. In object-oriented programming language, a class is an independent program unit, which has a class name, and the class name includes two main parts: attribute description and service description.
Object is an entity used to describe objective things in a system, and it is a basic unit of a system. An object consists of a set of attributes and a set of services that operate on this set of attributes. From a more abstract point of view, an object is an abstraction of something in the problem domain or implementation domain, which reflects the information that the thing needs to save and play in the system, and is the encapsulation of a group of attributes and a group of services that have the right to operate on these attributes. The objective world is composed of objects and their connections.
The relationship between class and object is just like the relationship between mold and casting, and the result of instantiation of class is object. The abstraction of class 1 objects is class. Class describes a set of objects with the same characteristics (properties) and the same behavior (methods).
For example, create the class A and call the method Sum of the class A in another class


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace Text 
{ 
 class A// Created class  
 { 
  public int Sum(int i, int j)// Methods encapsulated by classes  
  { 
   return i + j; 
  } 
 } 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   A a = new A();// Created class A Object of a 
   int p = 10; 
   int q = 10; 
   Console.WriteLine(a.Sum(p,q));// Calling class A Adj. Sum Method  
   Console.ReadLine(); 
  } 
 } 
  
} 

The output result is: 20

Above is about C # structure and class of all knowledge points, I hope to help you learn.


Related articles: