Given a class with an implicit conversion operator to a Guid:
class MyId
{
private readonly Guid innerGuid;
public MyId(Guid innerGuid)
{
this.innerGuid = innerGuid;
}
public static implicit operator Guid(MyId id)
{
return id.innerGuid;
}
}
When assigning to a Nullable<Guid>:
static void Main(string[] args)
{
MyId someId = null;
Guid? optionalId = someId;
Console.WriteLine("optionalId = " + (optionalId.HasValue ? optionalId.Value.ToString() : "NULL"));
}
I would expect the null reference to simply propagate across from someId to optionalId. ie. get the console output:
optionalId = NULL
However the compiler seems to take precedence on the Nullable's inner Guid type, and it attempts to execute the implicit conversion, which throws a NRE as the id parameter is obviously null.
Is this a bug or by design?
It can be fixed with a null coalescing operator and explicit null value:
static void Main(string[] args)
{
MyId someId = null;
Guid? optionalId = someId ?? (Guid?) null;
Console.WriteLine("optionalId = " + (optionalId.HasValue ? optionalId.Value.ToString() : "NULL"));
}
But that seems very weird. Resharper even fades it out, implying its unnecessary and states:
'??' right operand is always null
because you've a variable of type MyId, not of Guid? and you're assigning that variable to a Guid? and the MyId type has an IMPLICIT conversion operator. If you made it EXPLICIT then the compiler would probably complain about no conversion. What exactly would you expect to happen by assigning a null Foo class instance to a Guid? variable? Do you really expect that to be meaningful? –
Related
I have a class which contains a nullable strings, I want to make a check to see whether they stay null or somebody has set them.
simliar to strings, the class contains integers which are nullable, where i can perform this check by doing an equality comparison
with the .HasValue() method - it seems like strings dont have this?
So how do check whether it goes from null to notNull?
public class Test
{
public string? a
public string? b
public int? c
}
var oldQ = new Test(c=123)
var newQ = new Test(c=546)
bool isStilValid = newQ.c.HasValue() == oldQ.c.HasValue() //(this is not possible?)&& newQ.b.HasValue() == oldQ.b.HasValue()
why is this not possible?
HasValue property belongs to Nullable<T> struct, where T is also restricted to be a value type only. So, HasValue is exist only for value types.
Nullable reference types are implemented using type annotations, you can't use the same approach with nullable value types. To check a reference type for nullability you could use comparison with null or IsNullOrEmpty method (for strings only). So, you can rewrite your code a little bit
var oldQ = new Test() { c = 123 };
var newQ = new Test() { c = 456 };
bool isStilValid = string.IsNullOrEmpty(newQ.b) == string.IsNullOrEmpty(oldQ.b);
Or just use a regular comparison with null
bool isStilValid = (newQ.b != null) == (oldQ.b != null);
Only struct in C# have HasValue method, but you can simple create your own string extension as below and that will solve your problem.
public static class StringExtension {
public static bool HasValue(this string value)
{
return !string.IsNullOrEmpty(value);
}
}
I hope this is helpful for someone.
The equivalent comparing to null would be:
bool isStillValid = (newQ.c != null) == (oldQ.c != null) && (newQ.b != null) == (oldQ.b != null);
That's the equivalent to your original code, but I'm not sure the original code is correct...
isStillValid will be true if ALL the items being tested for null are actually null. Is that really what you intended?
That is, if newQ.c is null and oldQ.c is null and newQ.b is null and oldQ.b is null then isStillValid will be true.
The Nullable<T> type requires a type T that is a non-nullable value type for example int or double.
string typed variables are already null, so the nullable string typed variable doesn't make sense.
You need to use string.IsNullOrEmpty or simply null
int value=0;
if (value == 0)
{
value = null;
}
How can I set value to null above?
Any help will be appreciated.
In .Net, you cannot assign a null value to an int or any other struct. Instead, use a Nullable<int>, or int? for short:
int? value = 0;
if (value == 0)
{
value = null;
}
Further Reading
Nullable Types (C# Programming Guide)
Additionally, you cannot use "null" as a value in a conditional assignment. e.g...
bool testvalue = false;
int? myint = (testvalue == true) ? 1234 : null;
FAILS with: Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and '<null>'.
So, you have to cast the null as well... This works:
int? myint = (testvalue == true) ? 1234 : (int?)null;
UPDATE (Oct 2021):
As of C# 9.0 you can use "Target-Typed" conditional expresssions, and the example will now work as c# 9 can pre-determine the result type by evaluating the expression at compile-time.
You cannot set an int to null. Use a nullable int (int?) instead:
int? value = null;
int does not allow null, use-
int? value = 0
or use
Nullable<int> value
public static int? Timesaday { get; set; } = null;
OR
public static Nullable<int> Timesaday { get; set; }
or
public static int? Timesaday = null;
or
public static int? Timesaday
or just
public static int? Timesaday { get; set; }
static void Main(string[] args)
{
Console.WriteLine(Timesaday == null);
//you also can check using
Console.WriteLine(Timesaday.HasValue);
Console.ReadKey();
}
The null keyword is a literal that represents a null reference, one that does not refer to any object.
In programming, nullable types are a feature of the type system of some programming languages which allow the value to be set to the special value NULL instead of the usual possible values of the data type.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/null
https://en.wikipedia.org/wiki/Null
Declare you integer variable as nullable
eg: int? variable=0; variable=null;
I am using Eto gui framework. I saw some magic grammar in their source code;
for example:
int x;
int? x;
void func(int param);
void func(int? param);
What's different? I am confused.
and the symbol ? is hard to google.
It means they are Nullable, they can hold null values.
if you have defined:
int x;
then you can't do:
x = null; // this will be an error.
but if you have defined x as:
int? x;
then you can do:
x = null;
Nullable<T> Structure
In C# and Visual Basic, you mark a value type as nullable by using
the ? notation after the value type. For example, int? in C# or
Integer? in Visual Basic declares an integer value type that can be
assigned null.
Personally I would use http://www.SymbolHound.com for searching with symbols, look at the result here
? is just syntactic sugar, its equivalent to:
int? x is same as Nullable<int> x
structs (like int, long, etc) cannot accept null by default. So, .NET provides a generic struct named Nullable<T> that the T type-param can be from any other structs.
public struct Nullable<T> where T : struct {}
It provides a bool HasValue property that indicates whether the current Nullable<T> object has a value; and a T Value property that gets the value of the current Nullable<T> value (if HasValue == true, otherwise it will throw an InvalidOperationException):
public struct Nullable<T> where T : struct {
public bool HasValue {
get { /* true if has a value, otherwise false */ }
}
public T Value {
get {
if(!HasValue)
throw new InvalidOperationException();
return /* returns the value */
}
}
}
And finally, in answer of your question, TypeName? is a shortcut of Nullable<TypeName>.
int? --> Nullable<int>
long? --> Nullable<long>
bool? --> Nullable<bool>
// and so on
and in usage:
int a = null; // exception. structs -value types- cannot be null
int? a = null; // no problem
For example, we have a Table class that generates HTML <table> tag in a method named Write. See:
public class Table {
private readonly int? _width;
public Table() {
_width = null;
// actually, we don't need to set _width to null
// but to learning purposes we did.
}
public Table(int width) {
_width = width;
}
public void Write(OurSampleHtmlWriter writer) {
writer.Write("<table");
// We have to check if our Nullable<T> variable has value, before using it:
if(_width.HasValue)
// if _width has value, we'll write it as a html attribute in table tag
writer.WriteFormat(" style=\"width: {0}px;\">");
else
// otherwise, we just close the table tag
writer.Write(">");
writer.Write("</table>");
}
}
Usage of the above class -just as an example- is something like these:
var output = new OurSampleHtmlWriter(); // this is NOT a real class, just an example
var table1 = new Table();
table1.Write(output);
var table2 = new Table(500);
table2.Write(output);
And we will have:
// output1: <table></table>
// output2: <table style="width: 500px;"></table>
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Type result with Ternary operator in C#
I ran into this scenario, and there doesn't seem to be a natural way to return a nullable int. The code below gives compilation error because the ternary operator doesn't like null.
public int? userId
{
get
{
int rv;
return int.TryParse(userIdString, out rv) ? rv : null;
}
}
So you (or just me) really have to go all the way and spell it all out:
public int? userId
{
get
{
int id;
if(int.TryParse(userIdString, out id)){
return id;
}
return null;
}
}
EDIT: Is there a more natural way of instantiating a nullable, to make the ternary operator work?
public int? userId
{
get
{
int rv;
return int.TryParse(userIdString, out rv) ? (int?)rv : null;
}
}
public int? userId
{
get
{
int rv;
return int.TryParse(userIdString, out rv) ? (int?)rv : null;
}
}
EDIT: I hadn't read the question carefully enough; the problem isn't that the conditional operator doesn't like nulls - it's that it needs to know what the overall type of the expression should be... and that type has to be the type of either the left-hand side, or the right-hand side. null itself is a type-less expression which can be converted to many types; int is a perfectly valid type, but it's one of the types which null can't be converted to. You can either make the right-hand side explicitly of type int? and get the implicit conversion of int to int? from the left-hand side, or you can perform a cast on the left-hand side, and get the implicit conversion of null to int?.
My answer is like James's, but casting the null instead:
public int? userId
{
get
{
int rv;
return int.TryParse(userIdString, out rv) ? rv : (int?) null;
}
}
This is to emphasize that it's not a null reference; it's a null value of type int?. At that point, the conversion of the int rv is obvious.
There are two other alternatives along the same lines to consider though:
return int.TryParse(userIdString, out rv) ? rv : new int?();
return int.TryParse(userIdString, out rv) ? rv : default(int?);
Personally I think the "casted null" is the nicest form, but you can make up your own mind.
Another alternative would be to have a generic static method:
public static class Null
{
public static T? For<T>() where T : struct
{
return default(T?);
}
}
and write:
return int.TryParse(userIdString, out rv) ? rv : Null.For<int>();
I don't think I really like that, but I offer it for your inspection :)
You're returning a (non-nullable) int or null in the same expression. You'll need to explicitly return a int? in your ternary expression for that to work.
Is it possible to overload the null-coalescing operator for a class in C#?
Say for example I want to return a default value if an instance is null and return the instance if it's not. The code would look like something like this:
return instance ?? new MyClass("Default");
But what if I would like to use the null-coalescing operator to also check if the MyClass.MyValue is set?
Good question! It's not listed one way or another in the list of overloadable and non-overloadable operators and nothing's mentioned on the operator's page.
So I tried the following:
public class TestClass
{
public static TestClass operator ??(TestClass test1, TestClass test2)
{
return test1;
}
}
and I get the error "Overloadable binary operator expected". So I'd say the answer is, as of .NET 3.5, a no.
According to the ECMA-334 standard, it is not possible to overload the ?? operator.
Similarly, you cannot overload the following operators:
=
&&
||
?:
?.
checked
unchecked
new
typeof
as
is
Simple answer: No
C# design principles do not allow operator overloading that change semantics of the language. Therefore complex operators such as compound assignment, ternary operator and ... can not be overloaded.
This is rumored to be part of the next version of C#. From http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated
7. Monadic null checking
Removes the need to check for nulls before accessing properties or methods. Known as the Safe Navigation Operator in Groovy).
Before
if (points != null) {
var next = points.FirstOrDefault();
if (next != null && next.X != null) return next.X;
}
return -1;
After
var bestValue = points?.FirstOrDefault()?.X ?? -1;
I was trying to accomplish this with a struct I wrote that was very similar Nullable<T>. With Nullable<T> you can do something like
Nullable<Guid> id1 = null;
Guid id2 = id1 ?? Guid.NewGuid();
It has no problem implicitly converting id1 from Nullable<Guid> to a Guid despite the fact that Nullable<T> only defines an explicit conversion to type T. Doing the same thing with my own type, it gives an error
Operator '??' cannot be applied to operands of type 'MyType' and
'Guid'
So I think there's some magic built into the compiler to make a special exception for Nullable<T>. So as an alternative...
tl;dr
We can't override the ?? operator, but if you want the coalesce operator to evaluate an underlying value rather than the class (or struct in my case) itself, you could just use a method resulting in very few extra keystrokes required. With my case above it looks something like this:
public struct MyType<T>
{
private bool _hasValue;
internal T _value;
public MyType(T value)
{
this._value = value;
this._hasValue = true;
}
public T Or(T altValue)
{
if (this._hasValue)
return this._value;
else
return altValue;
}
}
Usage:
MyType<Guid> id1 = null;
Guid id2 = id1.Or(Guid.Empty);
This works well since it's a struct and id1 itself can't actually be null. For a class, an extension method could handle if the instance is null as long as the value you're trying to check is exposed:
public class MyClass
{
public MyClass(string myValue)
{
MyValue = myValue;
}
public string MyValue { get; set; }
}
public static class MyClassExtensions
{
public static string Or(this MyClass myClass, string altVal)
{
if (myClass != null && myClass.MyValue != null)
return myClass.MyValue;
else
return altVal;
}
}
Usage:
MyClass mc1 = new MyClass(null);
string requiredVal = mc1.Or("default"); //Instead of mc1 ?? "default";
If anyone is here looking for a solution, the closest example would be to do this
return instance.MyValue != null ? instance : new MyClass("Default");