Home  >  Article  >  Backend Development  >  C# Learning Diary 12---Data Types Reference Types (Preface)

C# Learning Diary 12---Data Types Reference Types (Preface)

黄舟
黄舟Original
2017-01-20 13:48:311466browse

We have finished studying the simple data type in C# - value type, and then we will learn another data type in C# - reference type. The word "reference" here means a variable of this type. It does not store the contained value directly, but points to the value it wants to store. That is to say, the reference type stores the address of the variable. There are 4 reference types in C#:

  • Class

  • Represents

  • Array

  • Interface

Here I will first give a general introduction to their respective meanings and definitions. Later I will write specific examples to introduce them in detail.

Class

Class is the basic unit of object-oriented programming. It is a data structure that contains data members, function members and nested types. The data members of a class include constants, fields, and events; function members include methods, properties, index indicators, operators, constructors, and destructors; classes and structures (structs) also contain their own members, but between them The main difference is that classes are reference types, while structures are value types. Classes support the inheritance mechanism. By inheriting derived classes, you can extend the data members and function methods of the base class, thereby achieving the purpose of code reuse and design reuse. (The inheritance and derivation of classes will be discussed in detail later, and the definition of classes will be written in the next article). When you define a class, you define a blueprint of a data type. This does not actually define any data (because it is an address), but it defines what the name of the class means, that is, what the object of the class consists of and what operations can be performed on this object. Objects are instances of classes. The methods and variables that make up a class become members of the class. If we define a variable for a certain class, we call it an instance of the class (it can also be called an object, but we will change the name later). Let me mention again that the two commonly used classes in C#, the Object class and the String class, will also be discussed in detail later.

stands for

The representative in C# (also called delegate, Delegate) is similar to the pointer in C/C++, but the pointer is unsafe in C/C++. There may be a Accidental deletion can cause the entire program to crash, and Delegate is safe in C#. Delegate refers to a certain method, which is derived from the System.Delegate class. When a Delegate is defined, it is an extension of System.Delegare. The specific usage will be discussed in detail later.

Array

When processing data in batches, we will use arrays. Like C/C++, an array is a set of ordered data of the same type. , a collection used to store data, described by the array name, type and dimension of the data elements. System.Array provided in C# is the base class for all array types. A specific element in the array is accessed by index. All arrays are composed of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.

Interface

The interface defines the syntax contract that all classes should follow when inheriting the interface. The interface defines the "what" part of the syntax contract, and the derived class defines the "how" part of the syntax contract. An interface defines properties, methods, and events, which are members of the interface. The interface only contains the declaration of members. Member definition is the responsibility of the derived class. Interfaces provide a standard structure that derived classes should follow. Abstract classes are similar to interfaces in a way, however, they are mostly used only when there are only a few methods declared by the base class and implemented by the derived class. (That is to say, the interface only provides method declarations, and the specific method is determined by the subclass that inherits it. For example, if I give 5 people 100 yuan each, what they do with the 100 yuan is none of my business. Only responsible for providing money) The interface is declared using the interface keyword, which is similar to the declaration of a class. Interface declarations are public by default. Multiple inheritance can be achieved through interfaces. (More details later)

The above is the content of C# Learning Diary 12---Reference Type of Data Type (Preface). For more related content, please pay attention to the PHP Chinese website (www.php .cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn