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

Sebastian
  • 4,625
  • 17
  • 76
  • 145
  • 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. – PMF Feb 25 '21 at 11:54
  • 1
    @PMF Strings are interned in C#, so assigning `""` also reuses the same instance. – Johnathan Barclay Feb 25 '21 at 11:55
  • It appears you are right: https://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or-to-intitialize-a-string I wasn't sure about this. – PMF Feb 25 '21 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 – Sebastian Feb 25 '21 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. – Matthew Watson Feb 25 '21 at 12:10
  • I want to set a string property LinkUrl only if p.LabelUrl is NOT NULL. Looks like if we use ?: it is mandatory to write the logic for both if and else cases In my case it is not really necessary to write the "" part – Sebastian Feb 25 '21 at 12:23
  • So you're actually looking for a shorthand for `if (LinkUrl != null) LinkUrl = "SET MY PROPERRTY TO A VALUE";` ? Or do you also need to handle the case that `LinkUrl` is not null but is whitespace (in which case set it to `""`)? – Matthew Watson Feb 25 '21 at 12:28
  • Looking for a shorthand of if(p.LabelUrl !=null) LinkUrl = "SET MY PROPERRTY TO A VALUE"; – Sebastian Feb 25 '21 at 12:32

2 Answers2

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);
Ivan Furdek
  • 711
  • 6
  • 6
0

You can use one of the Null-coalescing assignment.

But it won't check for empty strings.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59