In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to understand the nullable value type of C #, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Usually a value type variable can never be null, it always contains the value of the value type itself. However, in some cases, some problems will be encountered, such as: when mapping a C# nullable value column in a database, it becomes quite difficult to use Framework to process database data; in order to solve this problem, "C# nullable value type (nullable value type)" is introduced into CLR.
To understand how they work, take a look at the logic in CLR:
[Serializable, StructLayout (LayoutKind.Sequential)] public struct Nullable
< T>Where T: struct {private Boolean hasValue = false; / / indicates the state with one word paragraph, and the initial assumption is Null internal T value = default (T); public Nullable (T value) {this.value = value; this.hasValue = true;} public bool HasValue {get {return hasValue }} public T Value {get {if (! hasValue) throw new InvalidOperationException ("Nullable object must have a value."); return value;}} public T GetValueOrDefault () {return value } public T GetValueOrDefault (T defaultValue) {if (! HasValue) return defaultValue; return value;} public override bool Equals (object obj) {if (! HasValue) return (obj = = null); if (obj = = null) return false; return value.Equals (obj) } public override int GetHashCode () {if (! HasValue) return 0; return value.GetHashCode ();} public override string ToString () {if (! HasValue) return String.Empty; return value.ToString ();} public static implicit operator Nullable
< T>(t value) {return new Nullable
< T>(value);}}
Invocation and output:
Static void Main (string [] args) {Nullable
< Int32>X = 5; Nullable
< Int32>Y = null; Console.WriteLine ("x:HasValue {0}, value = {1}", x.HasValue, x.Value); Console.WriteLine ("y:HasValue {0}, value = {1}", y.HasValue, y.GetValueOrDefault ()); Console.ReadLine ();} output: x:HasValue True, value = 5 y:HasValue False, value = 0
Question marks are allowed in C # to declare initialization variables (equivalent to the above code) such as:
Int32? X = 5
Int32? Y = null
Summarize the interpretation of the operator by the C# nullable value type:
a. Unary operator if the Operand is null, the result is null
b. Any of the binary operators is null, and the result is null
c. Comparison operator if both operands are null, the two are equal; if one is null, the two are not equal; if neither is null, compare the value
Note: a large amount of code is generated when manipulating value types, similar to calls to base class (Nullable) code
/ / unary operator: (+ +--! ~) x examples; / / x = 6; y Murray; / / y = null; / / binary operator: (+-* /% & | ^
< < >>) x = x + 10; / / x = 15; y = y * 10; / / y = null / / comparison operator: (= =! =
< > < = >=) if (x = = null) Console.WriteLine ("x is null;"); else Console.WriteLine ("x is not null;"); if (y = = null) Console.WriteLine ("y is null;"); else Console.WriteLine ("y is not null;"); if (x! = y) Console.WriteLine ("x = y;"); else Console.WriteLine ("x! = y;"); if (x > y) Console.WriteLine ("x > y;"); else Console.WriteLine ("x
< = y;"); 当CLR对一个Nullable< T>When the instance is boxed, it will check whether it is null. If the boxing operation is not actually performed for null,CLR, a null value will be returned.
If the instance is not empty, the value is taken out of the type and boxed, such as:
Int32? A = null; object o = a; / an is null Console.WriteLine ("o is null = {0}", o = = null); / / "true" Int32? B = 5; o = b; / an is null Console.WriteLine ("oaths type = {0}", o.GetType ()); / / when "System.Int32" calls GetType on a nullable value type, CLR will spoof T instead of Nullable.
< T>When unboxing with a nullable value type, CLR allocates memory (this is a very special behavior, in all other cases, unboxing will never lead to memory allocation), because a boxed value type cannot be simply unboxed into a nullable version of the value type, and there is no Boolean hasValue field in the boxed value type, so CLR must allocate a Nullable when unpacking.
< T>Object, initialized hasValue = true, value = value type value. This can have an impact on the performance of the application.
Call the interface method through the C# nullable value type
Int32? N = 5; Int32 result = ((IComparable) n) .Compareto (5); Console.WriteLine (result); / / 0; the answer to the question on how to understand the nullable value type of C # is shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.