📜 ⬆️ ⬇️

How do you see .NET / C # 5.0?

Most recently, we received a .NET / C # 4.0 release. We talked a lot (and we will still talk) about new features and innovations of the new platform and language. But you can already draw some conclusions and think about what we want to see in the new version of .NET / C # 5.0?


Tuple Packaging and Unpackaging


In 4.0, a new data type Tuple has appeared, which can be used in this way:

var data = new Tuple < string , int > ( “John Doe”, 42 ) ;

It was great to use an abbreviated Tuple, for example:
')
public string , int , double ReturnMyTuple ( )
{
return "Hello World!" , 42 , 4.2 ;
}

or

public Tuple < string , int , double > ReturnMyTuple ( )
{
return "Hello World!" , 42 , 4.2 ;
}

// elsewhere: item1 is a string, item2 is an int, item3 is a double.
var item1, item2, item3 = ReturnMyTuple ( ) ;

Enums, Generics


It would be nice to pass enums to generic methods, for example:
public void DoSomething < T > ( T enum ) where T : System . Enum { ... }

You can also make enums classes (like it is in Java now).

Why not add generic support to generic methods?
T Add < T > ( T a, T b )
{
return a + b ;
}

In Keyword and Ranges


A simple way to check if a variable is in a specific set or range of values:
if ( x in ( 1 , 2 , 3 ) ) { }

if ( x in [ 5..10 ] ) { }

if ( x in [ 1 , 2 , 4..8, 10 ] ) { }

Extension properties


Itself sometimes thought, why not to do besides extension methods also properties-extensions. Those. instead
var value = someObject. Value ( ) ;

just write
var value = someObject. Value ;

Smart switch


Let's expand the switch to support expressions, conditions, etc .:
switch ( anInt )
{
case 1 , 2 :
Console. WriteLine ( "1 or 2" ) ;
break ;
case 3..9 :
Console. WriteLine ( "3 to 9" ) ;
break ;
case > = 10 :
Console. WriteLine ( "10 or higher" ) ;
break ;
default :
...
}

switch ( aString )
{
case "one" , "two" :
Console. WriteLine ( "1 or 2" ) ;
break ;
case "three" :
Console. WriteLine ( "3" ) ;
break ;
default :
...
}

switch ( aString )
{
case . IsNullOrEmpty ( ) :
...
case . Length > 100 :
...
case . Contains ( "foo" ) :
...
}


By the way, VB.NET supports this syntax.

Automatic flags at Enums


Basically, a good idea:
[ Flags ]
public enum MyFlags
{
Value1, // 1
Value2, // 2
ValueCombining1And2 = Value1 | Value2, // 3
Value3, // 4
Value4, // 8
ValueCombining3And4 = Value3 | Value4 // 12
}

More advanced null check


It is often necessary to use the property of some property of an object, which leads to a large series of checks. Why not do this:
MyClass value = null ;
int something = value. x . y ??? 0 ; // can you use a triple character here?
// something is now 0

Or for example instead of
var obj = Foo ( ) ;
Bar value = null ;
if ( obj. Bar ! = null && obj. Bar . Something ! = null )
{
value = obj. Bar . Something . DoSomething ( ) ;
}

write
var obj = Foo ( ) ;
var value = obj ? . Bar ? . Something ? . DoSomething ( ) ;

Exception grouping


Well, something like this:
try
{
}
catch ( ArgumentOutOfRangeException )
catch ( ArgumentNullException )
{
// Catch a ArgumentOutOfRangeException or a ArgumentNullException
}


Although something similar can be done now - handle the necessary exceptions in order of importance, and all the rest - with the help of a general Exception.

Still...


Multiple inheritance, virtual extension methods, the ability to process files with long names ... You can participate in the discussion of this issue on stackoverflow .

What would you like to see in .NET / C # 5.0 and which of the above ideas would you like to use?

Source: https://habr.com/ru/post/94182/


All Articles