Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the type basis of C #?

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you what the type basis of C# is, the content is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Let's first briefly review the type system in C#. Types in C # are divided into two categories, value types (ValueType) and reference types (ReferenceType). Value types and reference types are divided by how they are allocated in computer memory. Value types include structures and enumerations, and reference types include classes, interfaces, delegates, and so on. There is also a special value type called SimpleType, such as byte,int, which is actually an alias for the type of FCL class library, such as declaring an int type, which is actually declaring a System.Int32 structure type. Therefore, operations defined in the Int32 type can be applied to the int type, such as "123.Equals (2)".

All value types implicitly inherit from the System.ValueType type (note that System.ValueType itself is a class type), and System.ValueType and all reference types inherit from the System.Object base class. You cannot explicitly let the structure inherit a class, because C # does not support multiple inheritance, and the structure already inherits implicitly from ValueType.

Value Typ

When you declare a variable of a value type (Variable), the variable itself contains all the fields of the value type, and the variable is assigned on the thread stack (ThreadStack).

When we write a variable declaration statement like this in the program:

ValPointvPoint1

The actual effect is to declare the vPoint1 variable, which itself contains all the fields of the value type (that is, all the data you want).

Because the variable already contains all the fields of the value type, you can manipulate it at this time (manipulating the variable is actually a series of stack-in and out-stack operations).

We can solve this problem in such a way that the compiler implicitly creates a no-argument constructor for the structure type. In this constructor, structure members are initialized, all value type members are assigned 0 or equivalent (for Char types), and all reference types are assigned null values. Therefore, the Struct type cannot self-declare a constructor with no arguments. So, we can create a variable of type ValPoint through an implicitly declared constructor:

ValPointvPoint1=newValPoint ()

Console.WriteLine (vPoint.x); / / output is 0

Let's split the expression of the first sentence of the above code into two parts by "=":

On the left, ValPointvPoint1, create a variable vPoint of type ValPoint on the stack, with no values assigned to all members of the structure. Press vPoint on the stack before newValPoint ().

On the right, newValPoint (), the new operator does not allocate memory, but simply calls the default constructor of the ValPoint structure to initialize all fields of the vPoint structure according to the constructor.

The above content is what are the type basis of C#? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report