0

I am populating a Class using LINQ and LinkUrl is a string property of my class.I need to set a value only if a property is not null , other wise no need to assign any values Currently The conditional operator ?: is used

    var formattedData = dataset.Select(p => new  ListModel
    {
        Prop1=....,
        Prop2=....,
        ...
        LinkUrl = string.IsNullOrWhiteSpace(p.LabelUrl) ? "" : "SET MY PROPERRTY TO A VALUE",
        .....
    }).ToList();

Can we replace this with C#‘s null-coalescing operator (??) or (?.) or something similar ??

Tne intention is to avoid the repeated use of assigning to "" in so many places

I can write it with operator ?? , but handle cases of NULL only like below .

    LinkUrl = p.LabelUrl ?? "SET MY PROPERRTY TO A VALUE" 
    

Can we do something similar for Not null cases

8
  • 2
    If you use string.Empty instead of "", you never need to worry about assigning an empty string - it's free. The compiler always assigns the same instance, so that you don't waste any memory at all. Commented Feb 25, 2021 at 11:54
  • 1
    @PMF Strings are interned in C#, so assigning "" also reuses the same instance. Commented Feb 25, 2021 at 11:55
  • It appears you are right: stackoverflow.com/questions/263191/… I wasn't sure about this. Commented Feb 25, 2021 at 11:58
  • My intention in asking this question is to check any new features in c# available like ?? or ?. to replace the need of writing the logic on one side which is not necessary in this case . Looks like if we use ?: it is mandatory to write the logic for both if and else cases Commented Feb 25, 2021 at 12:08
  • 1
    I'm not sure what you actually want... if p.LabelUrl is null, your current code will replace it with "", but you say that you "need to set a value only if a property is not null" - which contradicts what your example code is actually doing. Commented Feb 25, 2021 at 12:10

2 Answers 2

1

I think closest you could get to a shorthand for not null would be a set of extension methods

    public static T NullOrValue<T>(this T nullable, T value) where T : class => nullable == null ? null : value;
    
    public static T? NullOrValue<T>(this T? nullable, T value) where T : struct => nullable == null ? (T?)null : value;

Which would then be available on any nullable object as

var value = anyObjectOrNullable.NullOrValue(MyValue);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use one of the Null-coalescing assignment.

But it won't check for empty strings.

3 Comments

Key req for me is to check the empty strings and i have to perform the actions only if the string is not empty
Then, it has nothing to do with null coalescing, right?
And you cannot conditionally assign in object initializers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.