This question already has answers here:
Nullable ToString()
(6 answers)
Why does .ToString() on a null string cause a null error, when .ToString() works fine on a nullable int with null value?
(8 answers)
Closed 4 years ago.
I have encountered the code something like this
int? x = new int();
x = null;
var y = x.toString();
My understanding is that it should throw a null reference exception. But the code is not breaking and I am getting the value of y as "". Please let me understand that what's happening here in behind.
Because it is not null.
You are setting null the value of Nullable<int>, which is designed to return an empty string if the value is null.
Related
This question already has answers here:
Nullable types and the ternary operator: why is `? 10 : null` forbidden? [duplicate]
(9 answers)
Closed 3 years ago.
Can anyone help me understand why the following doesnt compile?
string intAsString = "123";
int? nullableInt = null;
nullableInt = !string.IsNullOrEmpty(intAsString)
? Convert.ToInt32(intAsString)
: null;
As it complains that:
Type of conditional expression cannot be determined because there is no implicit conversion between int and null
I expected that since int? can be assigned null, this statement should work fine. It's easy enough to work around, but I'd like to understand why my expectation was wrong.
You need to explicitly cast the Int32 you get from Convert.ToInt32:
string intAsString = "123";
int? nullableInt = null;
nullableInt = !string.IsNullOrEmpty(intAsString) ? (int?)(Convert.ToInt32(intAsString)) : null;
The compiler finds out you have "int", and "null" as your result type. It is not smart enough to find a type fitting both. Cast one or the other explicitly to int?
This question already has answers here:
What is the purpose of a question mark after a value type (for example: int? myVariable)?
(9 answers)
What does the question mark in member access mean in C#?
(2 answers)
Closed 6 years ago.
I have a code containing a class with the following method:
public string AddEvent(DateTime date, int eventCount, int? eventId, string eventGuid){...}
My question is what does the question mark there do? My guess it has something to do with overloading since there's another overload method of AddEvent but I'm not sure...
Simple, this isn’t C++ code. It’s C#, where ? denotes a nullable value type. I.e. a value type that can also be null. int? is a shortcut for Nullable<int>.
Usually a simple type like int cannot be null, using ? allows the int to have a null value like an object.
Inside the function, you need to check to see if eventId is valid to use. You can do this by checking HasValue or equal to null. If you try to use it without checking, and it is null, then it will throw System.InvalidOperationException:
public string AddEvent(DateTime date, int eventCount, int? eventId, string eventGuid)
{
// use HasValue
if (eventId.HasValue)
eventId++;
// or check for null
if (eventId != null)
eventId++;
}
This question already has answers here:
C# elegant way to check if a property's property is null
(20 answers)
Closed 7 years ago.
I have the following C# problem.
Consider the function:
private String getStringFromNullabel()
{
var nullable = getClass(); // returns some object that could be null
if( nullable != null)
{
return nullable.Text;
}
return null;
}
This works but it is verbose and I would rather write something like:
private String getStringFromNullabel()
{
return NotThrowWrapper(getClass()).Text;
}
This will throw if getClass() returns null.
So I am looking for some syntax that is short enough that this stays a one-liner but rather returns null instead of throwing an exception.
Is there such a thing in C#?
Pre C#6
private String GetStringFromNullabel()
{
var nullable = getClass(); // returns some object that could be null
return nullable != null ? nullable .Text : null;
}
Post C# 6
private String GetStringFromNullabel()
{
return getClass()?.Text;
}
Note that you should follow the .NET naming conventions
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Nullable types and the ternary operator: why is `? 10 : null` forbidden?
Why doesn't this work? Seems like valid code.
string cert = ddCovCert.SelectedValue;
int? x = (string.IsNullOrEmpty(cert)) ? null: int.Parse(cert);
Display(x);
How should I code this? The method takes a Nullable. If the drop down has a string selected I need to parse that into an int otherwise I want to pass null to the method.
int? x = string.IsNullOrEmpty(cert) ? (int?)null : int.Parse(cert);
I've come across the same thing ... I usually just cast the null to (int?)
int? x = (string.IsNullOrEmpty(cert)) ? (int?)null: int.Parse(cert);
This question already has answers here:
Conditional operator assignment with Nullable<value> types?
(6 answers)
Nullable types and the ternary operator: why is `? 10 : null` forbidden? [duplicate]
(9 answers)
Closed 9 years ago.
Explain why a nullable int can't be assigned the value of null e.g
int? accom = (accomStr == "noval" ? null : Convert.ToInt32(accomStr));
What's wrong with that code?
The problem isn't that null cannot be assigned to an int?. The problem is that both values returned by the ternary operator must be the same type, or one must be implicitly convertible to the other. In this case, null cannot be implicitly converted to int nor vice-versus, so an explict cast is necessary. Try this instead:
int? accom = (accomStr == "noval" ? (int?)null : Convert.ToInt32(accomStr));
What Harry S says is exactly right, but
int? accom = (accomStr == "noval" ? null : (int?)Convert.ToInt32(accomStr));
would also do the trick. (We Resharper users can always spot each other in crowds...)
Another option is to use
int? accom = (accomStr == "noval" ? Convert.DBNull : Convert.ToInt32(accomStr);
I like this one most.
Similarly I did for long:
myLongVariable = (!string.IsNullOrEmpty(cbLong.SelectedItem.Value)) ? Convert.ToInt64(cbLong.SelectedItem.Value) : (long?)null;