Creating a custom operator - '!?' (negation of '??'). Is it possible? [duplicate] - c#

This question already has answers here:
Is it possible to create a new operator in c#?
(7 answers)
Closed 8 years ago.
Is it possible to create a custom operator like '!?' (negation of '??') instead of writing long expression:
int? value = 1;
var newValue = value != null ? 5 : (int?)null;
I want to have:
var newValue = value !? 5;
Thanks!

No.
You cannot create your own operators in C#. You can only override (some of the) ones that already exist in the language.

You cannot define new operator in C# - the only way is to redefine existing ones, but you can not redefine ?: and ?? operators.

Related

Assign if not null [duplicate]

This question already has answers here:
C# 6.0 Null Propagation Operator & Property Assignment
(3 answers)
Closed 2 years ago.
Being a fan of null-coalescing operator and the null-propagating operator, I would like to assign something if the object is not null.
Something like:
myArray.FirstOrDefault()?.Value = newValue;
Instead of:
var temp = myArray.FirstOrDefault()
if(temp != null){
temp.Value = newValue;
}
Is this possible?
(I am aware I could use SetValue() )
With C# 8.0 you can do this
temp ??= newValue;

Calling functions in C# ternary operator [duplicate]

This question already has answers here:
C# Conditional Operator Not a Statement?
(9 answers)
Closed 6 years ago.
Why is this code not valid? Pretty sure it's legit in C /C++
Pseudocode:
String s = Console.ReadLine();
int x = 0;
Int32.TryParse(s, out x) ? Console.WriteLine("Foo") : Console.WriteLine("bar");
The ternary operator is used to return values and those values must be assigned.
If you want to invoke void methods in a ternary operator, you can use delegates like this:
String s = Console.ReadLine();
int x = 0;
(Int32.TryParse(s, out x) ? new Action(() => Console.WriteLine("Foo")) : () => Console.WriteLine("bar"))();
console.writeline return void.. The conditional operator (?:) returns one of two values depending on the value of a Boolean expression
MSDN
As discussed here, in C#, not every expression can be used as a statement.

What does a ? do in a new Type?[5] assignment? [duplicate]

This question already has answers here:
What does "DateTime?" mean in C#?
(7 answers)
What does the bool? return type mean?
(3 answers)
Closed 9 years ago.
I have the following piece of code, and I have no idea what the ? means in the construct.
What is it doing?
myObj_Request r = new myObj_Request(); //instantiate intial object
r.Channels = new ChannelSequence(); //initial object has another object in channels
r.Channels.ChannelType = new ChannelType?[5] { .. } // ok so we have 5 channeltypes...
// but that ? infront of [5] ?
It's a nullable type. Difference?
int c = 5; // you can set only numbers
int? d = null; // you can set int or null
int? e = 5;
int? is basically exactly equal to Nullable<int>.
Here's the reference: MSDN
[edit]
So new ChannelType?[5] is an array (5 elements) of Nullable<ChannelType>.
Nullable type: check Link . You'll find more here.
"?" this will be used to make VeluTypes to NullableTypes
Ex:
Int is value type. You can't assign null to it. If you declare it like int?...you can assign null here

C# code won't compile. No implicit conversion between null and int [duplicate]

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);

c# why can't a nullable int be assigned null as a value [duplicate]

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;

Categories

Resources