The text of this article is not in this database — only its details are. Read it on the old site: Deep Dive to Nullable Types in C#
8 Comments
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
The text of this article is not in this database — only its details are. Read it on the old site: Deep Dive to Nullable Types in C#
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
Saineshwar BageriPosted Jun 6, 2014, 3:31 AM
Good article :)
Akhil MittalPosted Jun 4, 2014, 1:22 AM
Though the intention to clarify the concept was very clear, but there are few points I would like to take which I came across while reading the article, 1. I think the line "Here we will the some main properties of Nullable type and leave default ones" is not grammatically correct. 2. In the code snippet as mentioned as, "Now let's see what is inside this code: public Nullable(T value) { this.value = value; this.hasValue = true; } Here you can see that this is assigning a value to the variable and setting hasValue to true. Now let's see the HasValue property. public bool HasValue { get { return this.hasValue; } } So it actually has one property hasValue that does the entire job. By default it is set to false and once we assign a value to it, it is set to true. So whenever one accesses it without assigning it a value it returns a null reference." You say that in the default code this.hasValue is set to true as shown in code snippet, but after showing the HasValue Property you contradict your statement by saying that by default it was set to false, and you say whenever we access that value without assigning it it will give null reference exception.My question is why will it give null ref exception if in the code as shown here already contains a default value as true or false as per your statement? 3.In the code snippet, " "public override bool Equals(object other) { if (!this.HasValue) { return (other == null); } if (other == null) { return false; } return this.value.Equals(other); } This also checks the hasValue before comparing it." You say that it checks hasVlue, but actually its checking the property and not the class variable, so it should be HasValue. 4.In the code snippets as mentioned in below lines "First scenario Int? a=8; object o = a; long d = (long)c; It will be compiled successfully but will throw and an Invalid Cast Exception. In other words, you cannot cast it to any other type except the underlying type that is here int although int type can be held by long. But the following lines will run successfully: long d = (int)c; long? d = (int)c;" You talk about variable "c" but its not declared anywhere, the variable that you declared was Int? a, so please correct it to avoid confusion. 5.I tried to run the code Int? a=8; but it gives compile time error it takes int?a=8 and not capital Int.Please elaborate. 6. I executed the last code snippet, int? b=5; int? a=null; Console.WriteLine(a b); it writes nothing like you said, but when I assigned the a b vaue to var c as var c=a b and printed c, it showed the same behavior and in debug window it says c is null, but if i write Console.Writeline(null), it gives compile time error.Please explain this as this is a point of doubt. 7.There are some more grammatical mistakes in the post, please fix.
Pankaj BajajPosted Apr 30, 2014, 1:24 AM
really it's an very informative article..Thanks Brij.
Lakshmanan Sethu SankaranarayanPosted Apr 30, 2014, 1:02 AM
Good article :)
Mahesh ChandPosted Apr 29, 2014, 7:53 AM
Good start Brij. Yes agree with DJ. Formatting of code snippet is good in the editor using the Code tag. Also, people may want to copy the code snippet.
Former memberPosted Apr 29, 2014, 1:38 AM
I guess if we use code snippet instead of images then it would be more valuable. In between it is very informative post !