1

I was making a program in which I want to check if a string is equal to "", and if it isn't use it, but if it is, use the default value. I know I can do string != "" ? string : "default value", however, I feel like this is inefficient, as I keep typing out string. Of course, writing out string is no problem for me, but, if in another project, I have to reference a string with a long package name, it would be a little more annoying.

Orion31
  • 556
  • 6
  • 28
  • 1
    If you are checking null values you could use the ? operator a?.b?.c ?? "default". In your case, I would rather go for an extension method –  Nov 13 '17 at 00:51
  • @Seb I was looking into that, however my string is not null it is equal to “” – Orion31 Nov 13 '17 at 00:51
  • What is `a.b.c.d.string.toString()`? can you give an example? – Chetan Nov 13 '17 at 00:53
  • @ChetanRanpariya I meant `a.b.c.d` to be a long package name referencing an integer which I then had to convert to a string. Clarified in OP – Orion31 Nov 13 '17 at 00:56
  • If you're doing a lot of `a.b.c.d...` then your code is probably already a nasty mess and focussing on this is your least concern. – DavidG Nov 13 '17 at 00:58
  • @DavidG I was just trying to provide an example of when this would be useful. – Orion31 Nov 13 '17 at 00:58
  • Then write an extension method so you can do `stringVariable.DefaultIfEmpty("default")` – DavidG Nov 13 '17 at 01:03
  • not sure about this, but i thing checking `== string.empty` will be more efficient than `== ""`. Don't see why else there would be such a property – Neville Nazerane Nov 13 '17 at 01:03
  • @NevilleNazerane See this https://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and-empty-string – DavidG Nov 13 '17 at 01:06
  • @DavidG using the existing ternary operator would be more efficient than creating a new method for one use. I was just wondering if there was an operator like this. – Orion31 Nov 13 '17 at 01:06
  • @NevilleNazerane Didn't even know that existed. Thanks! – Orion31 Nov 13 '17 at 01:07
  • @Orion31 I'm just saying to write an extension method to wrap the ternery operator to "tidy" how the code looks. You're really not going to get any perforance boost doing it another way. – DavidG Nov 13 '17 at 01:07
  • You *"feel like this is inefficient"* - why? Just because the name of your string is long? You could just type this as a simple `if` statement: `if (myString == string.Empty) myString = defaultValue;` You don't really gain anything by evaluating it as a ternary statement. – slugster Nov 13 '17 at 01:08
  • @slugster I was just wondering if there was an operator that could do this, since writing `mystring == string.empty ? mystring : "default value"` didn't make much sense since I had to reference mystring twice. – Orion31 Nov 13 '17 at 01:09
  • Why are you worrying on the `namespace` because whatever implementation you will have, you have to include the namespace anyway whether in using `using` statement above or explicitly on the line of code. – Edper Nov 13 '17 at 01:10
  • 1
    One option is to just use an alias when your `string` is too long. `using shortString = my.too.long.string ` – fruggiero Nov 13 '17 at 01:11
  • @Edper I'm not worried about the namespace, I was just using an example to show what I was getting at. – Orion31 Nov 13 '17 at 01:12
  • check the link @DavidG send above. `mystring.Length == 0` has a better performance, and also you are not actually checking the value of your string twice – Neville Nazerane Nov 13 '17 at 01:12
  • @fruggiero I'm not worried about the actual length of the name of the string, I was just wondering if there was an operator in C# that could do what I mentioned above. – Orion31 Nov 13 '17 at 01:13
  • What exactly are you trying to fix here? Performance? Tidy code? If it's performance, you're almost certainly wasting your time trying to do microoptimisations like this. There is no operator that will provide the functionality you want. You would need to write your own. – DavidG Nov 13 '17 at 01:14
  • according to https://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.110).aspx, even the `IsNullOrEmpty` function uses `s == String.Empty` for checking. This means that even if you do find something, it isn't going to be any more efficient – Neville Nazerane Nov 13 '17 at 01:16

2 Answers2

4

At the moment there is no operator that can do what you want.

But there is actually a proposal for a "Default-or-Empty Coalesce operator", here:

https://github.com/dotnet/csharplang/issues/183

The best you can do at the moment is to declare an extension method like this:

    public static string NullIfEmpty(this string str)
    {
        return string.IsNullOrEmpty(str) ? null : str;
    }

and use it like this:

var foo = yourString.NullIfEmpty() ?? "default value";
fruggiero
  • 943
  • 12
  • 20
1

According to DavidG's option and helpful link, you could use this extension function.

public static string IfEmpty(this string str, string alternate)
{
    return str?.Length == 0 ? str : alternate;
}

(This will return the alternate string even if the 'str' is null)

And use as mystring.IfEmpty("something else")

Source: What is the difference between String.Empty and "" (empty string)?.

Also you don't need to reference a really long string.

fruggiero
  • 943
  • 12
  • 20
Neville Nazerane
  • 6,622
  • 3
  • 46
  • 79
  • 2
    This will throw if `str` is null of course. I would prefer to use `string.IsNullOrEmpty` – DavidG Nov 13 '17 at 01:16
  • I guess this means that no such operator like the one I described exists. – Orion31 Nov 13 '17 at 01:16
  • nop the `IsNullOrEmpty` comes the closest, but as I was saying in my other comment, won't improve performance – Neville Nazerane Nov 13 '17 at 01:18
  • Yes, but OP is so far refusing to admit if it's performance that is important here. I strongly suspect it's not. – DavidG Nov 13 '17 at 01:19
  • @DavidG When I meant efficiency, I didn't mean as in performance, but the repetition of typing the same thing again and again. I just thought it would be quirky if such a thing did exist, but in the long run, how much typing I do, or how many milliseconds it takes to process doesn't matter. – Orion31 Nov 13 '17 at 01:25
  • 1
    Then use `string.IsNullOrEmpty` and avoid runtime exceptions. – DavidG Nov 13 '17 at 01:26
  • 1
    @NevilleNazerane this will avoid the exception if str is null : `return str?.Length == 0 ? str : alternate;` – fruggiero Nov 13 '17 at 01:28
  • 1
    The ternary operator has the syntax `condition ? true_expression : false_expression` -- as written the answer will return an empty string of it's empty, or a default if it wasn't empty. This is the opposite of what you want. – Peter Torr - MSFT Nov 13 '17 at 01:37