In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are C # primitive types, value types, and reference types". The explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "what are C# primitive types, value types and reference types"!
First of all, understand what is the primitive type, the primitive type is the data type directly supported by the compiler, like we often use int string bool is the primitive type, the primitive type is mapped to the FCL type, like the previous three types corresponding to the FCL is Int32 String Boolean, the primitive type just provides convenience for our programming, it is exactly the same as the IL generated after compilation of the types in FCL. The corresponding tables of primitive types and FCL types are given below
We usually use primitive types when writing programs, because it is convenient, but the author of this book believes that we should directly use the types in FCL for the following reasons:
C # primitive type
FCL Typ
CLS compatibility
Description
Sbyte
System.Sbyte
Byte
System.Byte
Short
System.Int16
Ushort
System.UInt16
Int
System.Int32
Uint
System.Uint32
Long
System.Int64
Ulong
System.Uint64
Char
System.Char
Float
System.Single
Double
System.Double
Bool
System.Boolean
Decimal
System.Decimal
Object
System.Object
String
System.String
The FCL type corresponding to long in c # is System.Int64, but in other languages, such as C++, long is treated as an Int32. In this way, people who are used to one programming language will misunderstand when they look at the source code written in another programming language. Many methods in FCL have the type name as part of the method name, such as the System.Convert class providing ToBoolean,ToInt32,ToSingle, and so on. This makes it feel a little weird when using primitive types for type conversion, such as float val=Convert.ToSingle ("23").
Differences between value types and reference types in CLR VIA C # tutorials
1. All value types are derived from System.TypeValue, for example, Struct Enum is a value type; all reference types are derived from System.Object.
two。 The value type is assigned on the thread stack and the reference type is assigned on the managed heap.
3. Value types are represented in unboxed and boxed forms, while reference types are always boxed.
4. All value types are sealed types, so value types cannot be used as the base type of any type, nor can virtual methods be introduced into value types.
5. When you create a reference type variable, it is initialized to null, and when you try to use a reference type variable of null, a NullReferenceException exception is thrown, that is, the common "object reference is not set to an instance of the object"; when the value type is created, all members are initialized to 0, so the exception is not thrown.
The original example shows the difference between the value type and the reference type
Class Program {static void Main (string [] args) {SomeRef R1 = new SomeRef (); SomeVal v1 = new SomeVal (); r1.x = 5; v1.x = 5; Console.WriteLine (r1.x); / / 5 Console.WriteLine (v1.x); / / 5 SomeRef R2 = R1; SomeVal v2 = v1; r1.x = 8; v1.x = 9; Console.WriteLine (r1.x); / 8 Console.WriteLine (r2.x) / / 9 Console.WriteLine (v1.x); / / 9 Console.WriteLine (v2.x); / / 5}} class SomeRef {public Int32 x;} struct SomeVal {public Int32 x;}
Control of the layout of Type Fields in CLR of CLR VIA C # tutorial
To improve performance, clr can arrange fields of the type in any way you choose. We can change the order of this arrangement by using the System.Runtime.InteropServices.StructLayoutAttribute attribute on the structure of the class. This property accepts an enumerated value (Auto,Sequential,Explicit) of LayoutKind, and by default the C # compiler selects Atuo for the class and Sequential for the structure. Let's define a class and a structure
Struct SomeVal {public String name; public Int32 id;} class SomeRel {public String name; public Int32 id;} look at the IL code to see the default sort order
Now add attributes to the class and structure, and add the namespace using System.Runtime.InteropServices to the first reference
[StructLayout (LayoutKind.Auto)] struct SomeVal {public String name; public Int32 id;} [StructLayout (LayoutKind.Sequential)] class SomeRel {public String name; public Int32 id;} look at the IL and you can see that the order has changed.
The C # compiler always defaults to Sequential for value types such as structures, because value types often interact with unmanaged code, and fields must be consistent with the developer's definition. If we determine that the value type created will not interact with unmanaged code during the coding process, we can add [StructLayout (LayoutKind.Auto)] to the value type created as above and have it arranged automatically to improve performance.
Thank you for your reading, the above is the content of "what is the C# primitive type, value type, reference type". After the study of this article, I believe you have a deeper understanding of what the C# primitive type, value type and reference type are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.