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

How to understand C # value types and reference types

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to understand C # value types and reference types". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand C # value types and reference types.

The C# value type data stores data directly in the memory to which it is allocated, while the C# reference type simply contains a pointer to the location where the data is stored.

So which types are C# value types? I classify them into three categories:

Basic data types (except string types): including integer, floating point, decimal, Boolean.

Integers include: sbyte, byte, char, short, ushort, int, uint, long, ulong.

Floating-point types include float and double.

The decimal system is decimal

Boolean is bool.

Structure type: struct type

Enumerated type: enum type

There are five reference types: class, interface, delegate, object, string

The above is how to distinguish which C# value types and C# reference types, and there are differences in use. The data of all value types cannot be null and must be assigned an initial value after declaration; the reference type is allowed to be null.

Int I = 0; / / or int I = new int (); / / both of the above are correct and equivalent. Statements such as int I = null;// cannot be compiled because I is a value type and class c = null;// for null is not allowed so that it can be compiled, where c is a reference type and is allowed to be null

There is also a difference between value types and reference types when assigning (or copying) values. When assigning a value, the value type data copies the value directly to the new object, while the reference type simply copies a reference to the object. For example:

Public class abc {public int Attribute; public abc () {Attribute = 1;}} public static void Main () {int I = 0; int j = I; iCo1; Console.WriteLine ("I = {0}", I); Console.WriteLine ("j = {0}", j); / / the result is: / / iTun1 / / juni0 abc A1 = new abc (); abc a2 = A1; a1.Attribute = 2 Console.WriteLine ("A1 = {0}", a1.Attribute); Console.WriteLine ("a2 = {0}", a2.Attribute); / / the result is: / / A1: 2 / / A1: 2}

This example illustrates the difference between value types and reference types.

There is also the value C # type that is either assigned on the stack or inline in the structure. The C# reference type is assigned to the heap. Both the C # reference type and the C # value type are derived from the base class Object. When the C# value type needs to act as an object, assign a wrapper on the heap (which makes the value type look like a reference object) and copy the value of that value type to it. The wrapper is marked so that the system knows that it contains a value type. This process is called boxing, and the reverse operation is called unboxing. Boxing and unboxing enables any type to be treated like an object.

At this point, I believe you have a deeper understanding of "how to understand C # value types and reference types". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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: 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