nullable

When should one use nullable types in c#?

我是研究僧i 提交于 2019-12-17 18:18:05
问题 I have been repeatedly asked the following questions in many interviews.... But still can't explain them with a simple example... What are nullable types in c#? When should one use nullable types in c#? Can you give a simple example? Any suggestions? 回答1: From Using Nullable Types (C# Programming Guide) (Link updated circa 2018) For an example of when you might use a nullable type, consider how an ordinary Boolean variable can have two values: true and false. There is no value that signifies

Why does >= return false when == returns true for null values?

折月煮酒 提交于 2019-12-17 17:33:57
问题 I have two variables of type int? (or Nullable<int> if you will). I wanted to do a greater-than-or-equal (>=) comparison on the two variables but as it turns out, this returns false if both variables are null, while obviously the == operator returns true. Can someone explain to me why that is logical because the semantical definition of the >= operator contains the word "or"? 回答1: There was a huge debate about this oddity when the feature was originally designed back in C# 2.0. The problem is

Nullable bool fields in MS Access linked tables

纵饮孤独 提交于 2019-12-17 17:07:30
问题 Looks like I'm not the only one out there with this issue, but there doesn't seem to be an anwwer to this problem. I'm working in Access 2010, using a linked table to an SQL Server 2005 database (through an SQL Server ODBC pipe). In that table, one of the boolean fields is marked as nullable, and several records in this table do in fact have a null in the field. So far so good. In comes Access, and as soon as you open the linked table, Access shows a 0 (false) instead of a blank cell (problem

Variable type ending with ?

非 Y 不嫁゛ 提交于 2019-12-17 16:35:39
问题 What does ? mean: public bool? Verbose { get; set; } When applied to string? , there is an error: The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable' 回答1: ? makes your non-nullable (value) types nullable. It doesn't work for string , as it is reference type and therefore nullable by default. From MSDN, about value types: Unlike reference types, a value type cannot contain the null value. However, the nullable

C# - Basic question: What is '?'? [duplicate]

瘦欲@ 提交于 2019-12-17 16:16:47
问题 This question already has answers here : What does “DateTime?” mean in C#? (6 answers) Closed 6 years ago . I'm wondering what ? means in C# ? I'm seeing things like: DateTime? or int? . I suppose this is specific to C# 4.0? I can't look for it in Google because I don't know the name of this thing. The problem is I'm using DateTime and I have a lot of cast errors (from DateTime to DateTime? ). Thank you 回答1: It's a shorthand for writing Nullable<int> or Nullable<DateTime> . Nullables are used

Why Nullable<T> is a struct?

半腔热情 提交于 2019-12-17 16:14:19
问题 I was wondering why Nullable<T> is a value type, if it is designed to mimic the behavior of reference types? I understand things like GC pressure, but I don't feel convinced - if we want to have int acting like reference, we are probably OK with all the consequences of having real reference type. I can see no reason why Nullable<T> is not just boxed version of T struct. As value type: it still needs to be boxed and unboxed, and more, boxing must be a bit different than with "normal" structs

What is the memory footprint of a Nullable<T>

Deadly 提交于 2019-12-17 13:44:38
问题 An int ( Int32 ) has a memory footprint of 4 bytes. But what is the memory footprint of: int? i = null; and : int? i = 3; Is this in general or type dependent? 回答1: I'm not 100% sure, but I believe it should be 8 Bytes, 4 bytes for the int32, and (since every thing has to be 4-Byte aligned on a 32 bit machine) 4 bytes for a boolean indicating whether the integer value has been specified or not. On a 64 Bit machine it would still be 8 bytes (64 bits) since that is the smallest chunk of memory

How to change what default(T) returns in C#?

痞子三分冷 提交于 2019-12-17 12:16:32
问题 I would like to change how default(T) behaves for certain classes. So instead of returning null for my reference types I would like to return a null object. Kind of like kids.Clear(); var kid = kids.Where(k => k.Age < 10).SingleOrDefault(); if (kid is NullKid) { Console.Out.WriteLine("Jippeie"); } Anyone know if this is at all possible? 回答1: Anyone know if this is at all possible? It is simply not possible at all. But maybe you want to use DefaultIfEmpty instead: kids.Clear(); var kid = kids

Serializing a Nullable<DateTime> in to XML

不问归期 提交于 2019-12-17 10:47:30
问题 I am trying to serialize a class several of the data-members are Nullable objects, here is a example [XmlAttribute("AccountExpirationDate")] public Nullable<DateTime> AccountExpirationDate { get { return userPrincipal.AccountExpirationDate; } set { userPrincipal.AccountExpirationDate = value; } } However at runtime I get the error Cannot serialize member 'AccountExpirationDate' of type System.Nullable`1[System.DateTime]. XmlAttribute/XmlText cannot be used to encode complex types. However I

Optional return in C#.Net

有些话、适合烂在心里 提交于 2019-12-17 10:36:09
问题 Java 1.8 is receiving the Optional class, that allows us to explicitly say when a method may return a null value and "force" its consumer to verify if it is not null ( isPresent() ) before using it. I see C# has Nullable, that does something similar, but with basic types. It seems to be used for DB queries, to distinguish when a value exists and is 0 from when it doesn't exist and is null. But it seems that C#'s Nullable doesn't work for objects, only for basic types, while Java's Optional