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);
    }
3
  • 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. Commented Dec 9, 2016 at 23:00
  • Or you can avoid 2 extra lines by simply having "efg (x ?? "", y ?? "");" Commented Dec 9, 2016 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. Commented Dec 9, 2016 at 23:18

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?
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.

Comments

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.

2 Comments

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 = "").
My bad ! Updated the answer

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.