0

Ok, so I am new to C Sharp and I have one function call which might send null values. I want to make sure that if its null then I should set those null as empty string. Below is what I have tried and it works but I think there must be an elegant way to achieve this.

public static Concat ()
{ 
        // imagine x = null and y = null
        Abc( x, y);
}

public static Abc ( string x , string y)
{
       // blah blah
        x = x ?? "";
        y = y ?? "";
        efg( x, y);
}

I looked into this Solution but I could not understand it. Can I do something in the Abc arguments/parameter itself. I mean

public static Abc ( string x Can I do something here , string y Can I do something here)
    {
           // blah blah
            x = x ?? "";
            y = y ?? "";
            efg( x, y);
    }
Community
  • 1
  • 1
Unbreakable
  • 7,776
  • 24
  • 90
  • 171
  • No you cannot, as defined. If you allow passing a `string` (and `null` is a valid value for a string), then null can be passed as the parameter value. Your example snippet will do the trick though: `x = x ?? "";` can be read as "set x = x if x is not null, but if it's null, set it to blank string. – SlimsGhost Dec 09 '16 at 23:00
  • Or you can avoid 2 extra lines by simply having "efg (x ?? "", y ?? "");" – SlimsGhost Dec 09 '16 at 23:02
  • C# does not have support for non-nullable reference types. Suppose it hadm, then your method might have looked like `public static Abc(string! x, string! y)` where the exclamation marks would mean that the compiler guarantees these are never null. For some related suggestions for future C# features, see for example [Proposal: Nullable reference types and nullability checking](https://github.com/dotnet/roslyn/issues/5032). – Jeppe Stig Nielsen Dec 09 '16 at 23:18

3 Answers3

5

I think there must be an elegant way to achieve this.

There is: the ?? operator. You found it. Good on you.

Abc ( string x Can I do something here

You can say

public static Abc(string x = "", string y = "")

and now you can call it

Abc("a", "b);
Abc("a"); // actually calls Abc("a", "")
Abc();    // actually calls Abc("", "")

but this does not change the behaviour when null is passed in. It allows you to omit an argument, not change the value of a null argument.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Actually the original project that I am working on had the same thing `public static Abc(string x = "", string y = "")` in it. But because of null vaues I was getting issues in my DB. Can you tell me one thing. If I keep `public static Abc(string x = "", string y = "")`. Does this make the x and y as optional parameter. I mean I can simply call `Abc()` and it would still work right? – Unbreakable Dec 09 '16 at 23:08
0

Unfortunately, there is no compile time check supported natively by C# to enforce non-null values on reference types, including string.

You may provide a default value e.g. void DoSomething(string x = ""), however this does not prevent someone from overriding it with a null value.

Bas
  • 26,772
  • 8
  • 53
  • 86
  • Since `string.Empty` is not a `const`, it is not a compile-time constant and cannot be used for a default value. You can define your own `const` and use that, or you can go with the literal `""`, that is `void DoSomething(string x = "")`. – Jeppe Stig Nielsen Dec 09 '16 at 23:09
  • My bad ! Updated the answer – Bas Dec 12 '16 at 18:10
0

Minor improvement:

public static Abc ( string x , string y) => efg(x ?? "", y ?? "");

Not much different from you have already. The ? operator (as in someObject?.SomeProperty) returns the value of SomeProperty if someObject is not null. This does not apply in your example.

Ivan Vargas
  • 703
  • 3
  • 9