類別和結構是.NET Framework中的相同類型系統的兩種基本建構。兩者本質上都屬於資料結構,封裝這一組整體作為一個邏輯單位的資料和行為。資料和行為是該類別或結構的“成員”,它們包含各自的方法,屬性和事件等。
結構
結構為C#程式設計師用來定義自己的數值類型的最普遍的機制。結構比枚舉更強大,因為它提供方法,字段,操作符和訪問控制等。
結構與類別很相似,都表示可以包含資料成員和函數成員的資料結構。但是,與類別不同,結構是一種值類型,並且不需要堆分配。結構類型的變數直接包含了該結構的數據,而類別類型的變數所包含的只是對相應數據的一個引用(被引用的數據稱為「物件」)。
結構對於具有值語意的小型資料結構特別有用。複數,座標系中的點或字典中的“鍵-值”對都是結構的典型範例。這些資料結構的關鍵之處在於:他們只是少量資料成員,不要求使用繼承或引用標識,而且它們使用較方便(賦值時直接複製值而不是複製它的引用)。
結構的聲明透過關鍵字struct來實現,聲明格式為:
修飾 結構主體
};
結構宣告包含一組選用特性,並於接一個群組可選的結構修飾符,再跟關鍵字struct和一個用於命名結構的標識符,然後跟一個可選的結構接口規範,最後跟一個結構主體,根據需要後面還可以跟一個分號。
結構聲明可以根據需要包含結構修飾符:new,public,protected,internal,private
結構的使用結構的使用
結構的使用結構的使用
結構的使用
結構的使用
初始化實例字段也是錯誤的。初始化結構成員可透過兩種形式來完成:一是使用參數化建構函數,二是在宣告結構後分別存取成員。對於任何私有成員或以其他方式設定為不可存取的成員,只能在建構函式中進行初始化。
如果使用new運算子建立結構對象,則會建立該結構對象,並呼叫適當的建構子。與類別不同的是,結構的實例化可以不使用new運算子。在此情況下不存在建構函數的調用,因而可以提高分配效率。但是,在初始化所有欄位之前,欄位將保持未賦值狀態且物件不可用。
The declaration of a class in C# is instantiated through the class keyword. The format is:
Modifier class class name: base class or interface
{
Class body
}
Among them, "modifier" and ": base class or interface" are optional. The modifier of a class can be one of the following or a combination thereof (the same modifier is not allowed to appear multiple times in the class declaration ()).
(1)new: Only allowed to be used when declaring a nested class, indicating that the class hides members inherited from the base class and with the same name as the base class
(2 )public: Indicates that access to the class is not restricted
(3)internal: Only the class in which it belongs can access
(4) Private: Only applications or libraries in .NET can access it Access
(5)Abstract: Abstract class, creation of instances of the class is not allowed
(6)Sealed: Sealed class, inheritance is not allowed
Class inheritance statement: C# language only supports single inheritance
(1), constructor and destructor
C# provides a better mechanism to enhance program security sex. The C# compiler has a strict type safety check function, which can find almost all syntax problems in the program. However, just because the program passes the compilation check does not mean that the error no longer exists.
The C# language fully considers the occurrence of program errors and solves them well, that is, placing the object initialization work in the constructor and the cleanup work in the destructor. When an object is created, the constructor is executed automatically. When the object dies, the destructor is executed automatically.
The name of the constructor cannot be chosen casually. It must be recognized by the compiler before it can be automatically executed. Its naming method is simple and reasonable: let the constructor have the same name as the class. In addition to the name, another special feature of the constructor is that it has no return value type, which is different from the function whose return value type is void.
The destructor is a method member that destroys an instance of a class. The destructor cannot have parameters, cannot have any modifiers and cannot be called. The purpose of the destructor and the constructor is different, so the destructor is prefixed with "~" to show the difference.
Although the constructor and destructor are formally simpler functions in a class. But their use is by no means as simple as it seems, so using constructors and destructors flexibly and correctly can help users better understand the CLR's memory management mechanism and better manage resources in the system.
(2), Classes and Objects
A class is a collection of objects with the same attributes and services. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: properties and services. In object-oriented programming languages, a class is an independent program unit, which has a class name. The class name includes two main parts: attribute description and service description.
An object is an entity used to describe objective things in the system and is a basic unit that constitutes the system. An object consists of a set of properties and a set of services that operate on the set of properties. From a more abstract perspective, an object is an abstraction of something in the problem domain or implementation domain. It reflects the information that the thing needs to save and the role it plays in the system. It is a set of attributes and the right to perform operations on these attributes. A wrapper for a set of services that operates. The objective world is composed of objects and the connections between objects.
The relationship between a class and an object is like the relationship between a mold and a casting. The result of instantiation of a class is an object. The abstraction of a type of object is a class. A class describes a group of objects with the same characteristics (properties) and the same behavior (methods).
For more articles related to C# structures and classes, please pay attention to the PHP Chinese website!