-2

When MultiVal is null, I want it to be replaced by a space " ", how can I do it?

var selectedComponents=string.Join(',', MultiVal);
sb.Replace("{components}",selectedComponents);
maya
  • 27
  • 6
  • Perhaps `var selectedComponents=string.Join(',', MultiVal??" ");`, See: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator – Ibrennan208 Aug 10 '22 at 18:55

4 Answers4

1

If I understood correctly

var selectedComponents = MultiVal == null ? " " : string.Join(',', MultiVal);
sb.Replace("{components}", selectedComponents);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
yassinMi
  • 707
  • 2
  • 9
0

You could do something like:

var selectedComponents = string.Join(',', MultiVal ?? "");
sb.Replace("{components}",selectedComponents);
Henrique
  • 51
  • 2
0
var selectedComponents = string.Join(',', MultiVal ?? new string[] {" "});
0

Hope you are looking for something like this. If MultiVal is not null you can place another string where it says string.Empty:

var selectedComponents = string.Join(",", MultiVal == null? " " : string.Empty);

Or you can just simply check if MultiVal is null:

    if (MultiVal == null)
    {
        selectedComponents = string.Join(",", " ");
    }