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 are the C# type systems?

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces what the C# type system has, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Let's first briefly review the C# type system. The C # type system is divided into two categories, one is Value Type and the other is Reference Type. 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 Simple Type, 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.

1. 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 (Thread Stack). If we have such a value type, it represents a point on the line:

Public struct ValPoint {public int x; public ValPoint (int x) {this.x = x;}}

two。 Reference type

When a reference type variable is declared, the variable of the reference type is assigned to the stack, which is used to hold the memory address of the instance of the reference type on the heap, and the variable itself does not contain the data of the object. At this point, if only one variable is declared, since no instance of the type has been created on the heap, the value of the variable is null, which means that it does not point to an instance of any type (objects on the heap). For the type declaration of a variable, it is used to limit the types that the variable can hold.

If we have a class like this, it still represents a point on the line:

Public class RefPoint {public int x; public RefPoint (int x) {this.x = x;} public RefPoint () {}}

3. About simple types

Many articles and books like to use an int type as a value type and an Object type as a reference type when describing such problems. In this article, we will use a custom structure and class to describe the value type and the reference type, respectively. This is because simple types (such as int) have behaviors implemented by CLR that can mislead us about some operations.

For example, if we want to compare whether two int types are equal, we usually do this:

Int I = 3; int j = 3; if (i equals to j) Console.WriteLine ("i equals to j")

In fact, as we'll see later, when using "=" to compare reference type variables, they compare whether they point to the same object on the heap. The above an and b obviously point to different objects, but the objects contain the same values, so it can be seen that for string types, CLR's comparison of them is actually a value, not a reference.

In order to avoid the confusion caused by the above, custom structures and classes will be used in object judgment and other parts.

Packing and unpacking

This part can be deep or shallow, and this article only makes a brief review. Simply put, boxing is converting a value type to an equivalent reference type. Its process is divided into several steps:

1. Allocate memory on the heap for a newly generated object that contains data and does not have a name of its own.

two。 Copies the value of the value type variable on the stack to the object on the heap.

3. Returns the address of the object created on the heap to the reference type variable (from the programmer's point of view, the name of this variable is like the name of the object on the heap).

Thank you for reading this article carefully. I hope the article "what are the C# type systems" shared by the editor will be helpful to everyone? at the same time, I also hope that you will support and pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

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: 299

*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