-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


C#,

, 27 2017 . 07:53 +


2003- C# 1.2 major .
, roslyn github, 7.1 7.2.

, 7.1 pre-release Visual Studio.

.



.

Main ( 7.1)


, .
Main void int. . :

public static Task Main();
public static Task Main();
public static Task Main(string[] args);
public static Task Main(string[] args);

CLR entrypoints, boilerplate code ( - , void Task)
:

async Task Main(string[] args) {
    //  -  
}
//      
int $GeneratedMain(string[] args) {
    return Main(args).GetAwaiter().GetResult();
}

7- .

default ( 7.1)


Visual Basic C# . .
C# null, VB.NET Nothing. , Nothing , . , null . , null.

default(T).
. , , :

void SomeMethod(string[] args)
{
            
}

:

SomeMethod(default(string[]));

default(string[]), default?

, C# 7.1 default.
, ?
, :

void SomeMethod(string[] args = default)
{
            
}

:

int i = default;

:

int i = 1; 
if (i == default) { }   //     int  0
if (i is default) { }     //    

? default :

const int? y = default;  
if (default == default)
if (default is T) //  is    default
var i = default
throw default
default as int; // 'as'    reference 

Readonly ref ( 7.2)


by value ( ) , . . ref , , . vector matrix.
:

    static Vector3 Add (ref readonly Vector3 v1, ref readonly Vector3 v2)
    {
        //  !
        v1 = default(Vector3);

        //   !
        v1.X = 0;

        //   !
        foo(ref v1.X);

        //    
        return new Vector3(v1.X +v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
    }

, .
in ref readonly

    static Vector3 Add (in Vector3 v1, in Vector3 v2)
   {
       return new Vector3(v1.X +v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
   }

in, ref readonly ? in .

( 8.0)


.
, - , . (!) .
.
Java 8 , C# .
API .
.
SomeMethod :

interface IA
{
 void SomeMethod() { WriteLine(" SomeMethod  IA"); }
}

, :

class C : IA { }

:

IA i = new C();

SomeMethod:

i.SomeMethod(); //    " SomeMethod  IA"

( 7.1)


. .

:
, (f1: x.f1, f2: x?.f2) (x.f1, x?.f2).
f1, f2
( item1, item2 ...)

LINQ

// c  result       f1  f2
var result = list.Select(c => (c.f1, c.f2)).Where(t => t.f2 == 1);


C# 7.0. :

Action y = () => M();
var t = (x: x, y);
t.y(); //     extension method y(),     

, 7.0 7.1 .
, Visual Studio 7.0 ,
Tuple element name 'y' is inferred. Please use language version 7.1 or greater to access an element by its inferred name.

:

class Program
{
   static void Main(string[] args)
   {
       string x = "demo";
       Action y = () => M();
       var t = (x: x, y);
       t.y(); //     extension method y(),       
   }

   private static void M()
   {
       Console.WriteLine("M");
   }
}

public static class MyExtensions
{
   public static void y(this (string s, Action a) tu)
   {
       Console.WriteLine("extension method");
   }
}

.NET 4.6.2 , NuGet System.ValueTuple
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/331554/

:  

: [1] []
 

:
: 

: ( )

:

  URL