nullable

How to parse a string into a nullable int in C# (.NET 3.5)

I'm wanting to parse a string into a nullable int in C#. ie. I want to get back either the int value of the string or null if it can't be parsed. I was kind of hoping that this would work int? val = stringVal as int?; But that won't work, so the way I'm doing it now is I've written this extension method public static int? ParseNull...

C#: Is there any difference between bool? and Nullable<bool> ?

In C# are the nullable primitive types (i.e. bool?) just aliases for their corresponding Nullable<T> type or is there a difference between the two? ...

Windows Forms Designer upset by a control with a nullable property

Hi, I have a "numeric textbox" in C# .NET which is nothing more than a derivation of Textbox, with some added logic to prevent the user entering anything non-numeric. As part of this, I have added a Value property of type double? (or Nullable<double>). It's nullable to support the case where the user doesn't enter anything. The control...

Conditional operator assignment with Nullable<value> types?

EmployeeNumber = string.IsNullOrEmpty(employeeNumberTextBox.Text) ? null : Convert.ToInt32(employeeNumberTextBox.Text), I often find myself wanting to do things like this (EmployeeNumber is a nullable as it's a property on a LINQ-to-SQL dbml object where the column allows NULL values). Unfortunately, the compiler fe...

What does "DateTime?" mean in C#?

I am reading a .Net book, and in one of the code examples there is a class definition with this field: private DateTime? startdate What does "DateTime?" mean? ...

What's the difference between 'int?' and 'int' in C#?

I am 90% sure I saw this answer on stackoverflow before, in fact I had never seen the "int?" syntax before seeing it here, but no matter how I search I can't find the previous post, and it's driving me crazy. It's possible that I've been eating the funny mushrooms by accident, but if I'm not, can someone please point out the previous po...

Proper nullable type checking in C#?

Ok, my actual problem was this: I was implementing an IList<T>. When I got to CopyTo(Array array, int index), this was my solution: void ICollection.CopyTo(Array array, int index) { // Bounds checking, etc here. if (!(array.GetValue(0) is T)) throw new ArgumentException("Cannot cast to this type of Array."); // Handl...

C# nullable string error

private string? typeOfContract { get { return (string?)ViewState["typeOfContract"]; } set { ViewState["typeOfContract"] = value; } } Later in the code I use it like this: typeOfContract = Request.QueryString["type"]; I am getting the following error at the declaration of typeOfContract line stating "The t...

Get null == null in SQL

I wish to search a database table on a nullable column. Sometimes the value I'm search for is itself NULL. Since Null is equal to nothing, even NULL, saying where MYCOLUMN=SEARCHVALUE will fail. Right now I have to resort to where ((MYCOLUMN=SEARCHVALUE) OR (MYCOLUMN is NULL and SEARCHVALUE is NULL)) Is there a simpler way of sa...

How do I use DateTime.TryParse with a Nullable<DateTime>?

I want to use the DateTime.TryParse method to get the datetime value of a string into a Nullable. But when I try this: DateTime? d; bool success = DateTime.TryParse("some date text", out (DateTime)d); the compiler tells me "'out' argument is not classified as a variable". Not sure what I need to do here. I've also tried: out (Dat...

.NET Dynamic Objects with Reflection

How do I determine if a Nullable(of Enum) is indeed an Enum by means of reflection? I'm working with a method that dynamically populates an object of type T with an IDataReader retrieved from a database call. At its essence, it loops through the datareader's ordinals, and all the properties of T and populates the properties that match ...

Why is this code invalid in C#?

The following code will not compile: string foo = "bar"; Object o = foo == null ? DBNull.Value : foo; I get: Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string' To fix this, I must do something like this: string foo = "bar"; Object o = foo == null ?...

Initialising a struct variable to Nullable<int>

Hi, Is there a way to declare a variable as Nullable in c#? struct MyStruct { int _yer, _ner; public MyStruct() { _yer = Nullable<int>; //This does not work. _ner = 0; } } ...

Nullable GUID

In my database, in one of the table I have a GUID column with allow nulls. I have a method with a Guid? parameter that inserts a new data row in the table. However when I say myNewRow.myGuidColumn = myGuid I get the following error: "Cannot implicitly convert type 'System.Guid?' to 'System.Guid'." ...

C# ADO.NET: nulls and DbNull -- is there more efficient syntax?

I've got a DateTime? that I'm trying to insert into a field using a DbParameter. I'm creating the parameter like so: DbParameter datePrm = updateStmt.CreateParameter(); datePrm.ParameterName = "@change_date"; And then I want to put the value of the DateTime? into the dataPrm.Value while accounting for nulls. I thought initially I'd b...

In C# why can't a conditional operator implicitly cast to a nullable type

I am curious as to why an implicit cast fails in... int? someValue = SomeCondition ? ResultOfSomeCalc() : null; and why I have to perform an explicit cast instead int? someValue = SomeCondition ? ResultofSomeCalc() : (int?)null; It seems to me that the compiler has all the information it need to make an implicit casting decision, n...

History of VB.NET Nullable syntax

I can't find a definitive answer. Since C# 2.0 you've been able to declare int? i = 125; as shorthand for Nullable<int> i = Nullable<int>(123); I recall reading somewhere that VB.NET did not allow this shortcut. But low and behold, I tried it in VS 2008 today and it works. Does anyone know whether it's been this way since .NET 2...

Using nullable types in C#

I'm just interested in people's opinions. When using nullable types in C# what is the best practice way to test for null: bool isNull = (i == null); or bool isNull = !i.HasValue; Also when assigning to a non-null type is this: long? i = 1; long j = (long)i; better than: long? i = 1; long j = i.Value; ...

Nullable type issue with ?: Conditional Operator

Could someone explain why this works in C#.NET 2.0: Nullable<DateTime> foo; if (true) foo = null; else foo = new DateTime(0); ...but this doesn't: Nullable<DateTime> foo; foo = true ? null : new DateTime(0); The latter form gives me an compile error "Type of conditional expression cannot be determined because there is no ...

Nullable<T>: and overloaded operators, bool? & bool

Why is operator '&' defined for bool?, and operator '&&' is not? How exactly does this 1) bool? & bool? and 2) bool? and bool work? Any other "interesting" operator semantics on Nullable? Any overloaded operators for generic T? ...