This question already has answers here:
Nullable types: better way to check for null or zero in c#
(13 answers)
Closed 5 years ago.
Our application requires int? variables. I'm often checking both to make sure not null and not 0 and it does get lengthy.
Is there an out of the box version of String.IsNullOrWhiteSpace() or String.IsNullOrEmpty() for int?
Maybe this would require an extension method?
If there even was or when someone makes one, would something like this considered bad practice?
I don't think so but it's easy to write your own:
[Pure]
public static bool IsNullOrDefault<T>(this T? pValue)
where T : struct {
return pValue == null || pValue.Value.Equals(default(T));
// or as suggested in comments (tested)
return pValue == null || EqualityComparer<T?>.Default.Equals(pValue, default(T));
}
Related
This question already has answers here:
Why do I have to typecast an int in a ternary expression? [duplicate]
(4 answers)
Closed 5 years ago.
I have following field in my class:
public bool? EitherObject1OrObject2Exists => ((Object1 != null || Object2 != null) && !(Object1 != null && Object2 != null)) ? true : null;
But in Visual Studio I get an IntelliSense error that there cannot be a conversion between "bool" and "NULL" even though the field is nullable.
What do I do wrong?
And is there a cleaner way to check if either one of two objects is not null but one of them must be null?
try
? (bool?) true : null;
the problem is that default bool (true) is not nullable so the case statement are returning different types as far as the compiler is concerned
And you can remove the redundancy as pointed out by Servy
Object1 != null ^ Object2 != null
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:
Overloading null ambiguity
(2 answers)
Closed 7 years ago.
I have two functions which differ only in their second parameter.
Example:
public IEnumerable<Thing> Get(string clause, List<Things> list)
{
}
public IEnumerable<Thing> Get(string clause, List<OtherThing> list)
{
}
I want to call the first instance of this function, but I want to pass null as the second parameter. Is there a way to specify the "type" of null?
Cast the null literal:
Get("", (List<Things>)null)
store it in a variable first:
List<Things> list = null;
Get("", list);
Use reflection. (I'm not going to show an example because it's needlessly complicated.)
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;