Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TryParse create inline parameter?

Is there any way in C# to create a variable inline? Something like this:

int x = int.TryParse("5", out new int intOutParameter) ? intOutParameter : 0;

Don´t you think that this is more useful than creating a variable outside and then never use it again?

like image 728
Anton Selin Avatar asked Nov 04 '14 13:11

Anton Selin


People also ask

What is inline variable in C#?

Inline variable declaration (IDE0018)This style rule concerns whether out variables are declared inline or not. Starting in C# 7, you can declare an out variable in the argument list of a method call, rather than in a separate variable declaration.

How to use TryParse for string in c#?

TryParse(String, NumberStyles, IFormatProvider, Single) Converts the string representation of a number in a specified style and culture-specific format to its single-precision floating-point number equivalent. A return value indicates whether the conversion succeeded or failed.

What is the difference between Parse and TryParse in c#?

The Parse method returns the converted number; the TryParse method returns a boolean value that indicates whether the conversion succeeded, and returns the converted number in an out parameter. If the string isn't in a valid format, Parse throws an exception, but TryParse returns false .


2 Answers

That syntax – called declaration expressions – was on the proposed feature list for the next version of C# (version 6).

You're not the only one to think it is useful. For instance making a complete TryParse call an expression (no need for a statement to declare the variable).

However it has been dropped from the ongoing work to C#6.

I'm sure I'm not the only one hoping it will make a return in a future version.It is included in C#7 as a declaration (no need for new):

int x = int.TryParse("5", out int intOutParameter) ? intOutParameter : 0;
like image 92
Richard Avatar answered Oct 14 '22 08:10

Richard


Inline declarations for out params is a new suggested feature in C# that might be standard one day, see e.g. Probable C# 6.0 features illustrated, section 9. The expected/proposed syntax:

int.TryParse("5", out int x); // this declares (and assigns) a new variable x

Edit: This out variable syntax was eventually included in C# 7.0 (Visual Studio 2017); you can also use out var x.


Addition: People come up with fun extension methods. I tried to make a generic one:

public delegate bool TryParser<TResult>(string s, out TResult result);

public static class FunExtensions
{
  public static T TryParse<T>(this string str, TryParser<T> tryParser)
  {
    T outResult;
    tryParser(str, out outResult);
    return outResult;
  }
}

This can be used like this:

  var x = "5".TryParse<int>(int.TryParse);
  var y = "01/01".TryParse<DateTime>(DateTime.TryParse);
  var z = "bad".TryParse<decimal>(decimal.TryParse);

and so on. I was hoping the compiler would infer T from usage, so that one could say simply:

  var x = "5".TryParse(int.TryParse);  // won't compile

but it appears you have to explicitly specify the type argument to the method.

like image 20
Jeppe Stig Nielsen Avatar answered Oct 14 '22 09:10

Jeppe Stig Nielsen