Reputation: 333
I am just curious if this is possible or is there a way for this to become a valid syntax for C#:
expression == value ? /*do nothing here, or put some empty block like { ; } */ : SomeClass.SomeMethod();
Edit: For an in-depth discussion and more information, I thought this block would work (if the dictionary key tested does not exist, it adds the dictionary. else, it would skip):
(!packageDict.ContainsKey(desc)) ? packageDict.Add(desc, subtotal) : /*does nothing*/;
Upvotes: 7
Views: 16224
Reputation: 9630
The tricky way!!
as per discussion in comment section
this is very basic requirement when working with Dictionary
.. you need to check these type of logical test may times .. so i would like to suggest you to write some extension methods
which can make program smother ..
public static class Extended
{
public static void AddKeyIfNotExists<T, V>(this IDictionary<T,V> dictionary, T key, V Value)
{
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, Value);
}
}
}
Later you can use it in your program seamlessly
Dictionary<string,decimal> packageDict= new Dictionary<string,decimal>();
packageDict.AddKeyIfNotExists("demoKey",500);//will add in dictionary
packageDict.AddKeyIfNotExists("demoKey1", 200);//will add in dictionary
packageDict.AddKeyIfNotExists("demoKey", 6);//will not add in dictionary
Upvotes: 1
Reputation: 45119
By looking at your code snippet, it simply makes no sense to use a ternary operation here.
All you can do is:
if (!packageDict.ContainsKey(desc))
packageDict.Add(desc, subtotal)
Upvotes: 6
Reputation: 273510
This is not possible and makes little sense.
The ternary operator is supposed to return a value based on a condition. This is very important. You can treat it as something like this:
public static SomeType TernaryOperator<T>(Predicate<T> condition) {
if (condition()) {
return something;
} else {
return somethingElse;
}
}
So if you remove the return something
part, will the code compile? The answer is an undoubtedly no. What will the method return if the condition is true?
The same thing applies to ternary operators. If the condition evaluates to true, what will the whole expression return? It is undefined!
In fact, having this idea in the first place is pretty strange. Why would you want to do that? You can just use an if statement.
if (condition) {
// do nothing
} else {
//something
}
Of course, you can always do it like this:
if (!condition) {
// something
}
to remove the useless curly braces.
When you encounter this kind of situation next time, just use an if statement. It is more suitable.
Upvotes: 1
Reputation: 157098
No, and that makes sense. What would expression
be in your no-operation action? null
? The same as it was? How should the compiler know that?
This for example will leave the variable unset. What is expression
here?
var expression == value ? : SomeClass.SomeMethod();
Just make explicit what you expect, so either this:
expression == value ? null : SomeClass.SomeMethod();
expression == value ? default(Expression) : SomeClass.SomeMethod();
Or this to keep the variable the same when value
is true:
expression == value ? expression : SomeClass.SomeMethod();
if (!value) expression = SomeClass.SomeMethod();
Upvotes: 5
Reputation: 23088
You should use an IF
statement (it is also the most readable option).
An alternative is to use default for your type (e.g. null
for reference types or default(value_type)
for value types. E.g:
expression == value ? (int?) null : SomeClass.SomeMethod();
Upvotes: 1