余分なものはありますか?

すべてが高速になります。 Anatoly Levenchukのこのパフォーマンスは、最近私を悩ませています。 昨年のディープラーニングの成功は、すべてが非常に急速に変化することを示唆しています。 あまりにも長い間、 叫んでいるオオカミに対して、オオカミは「人工知能」と言いましたが、彼はそこにいませんでした。 そして、彼がついに私たちのところに来たとき、多くの人々は単にこれを認識せず、すべてが次の知的ゲームでのコンピューターの次の勝利で終わると考えています。 すべてではないにしても、多くの人々は進歩から除外されます。 そして、このプロセスはすでに実行されています。 現時点では、記事のタイトルにある質問にはあまり興味がないと思います。 しかし、この瞬間はまだ到来していませんが、私はこの潜在的に物議を醸す問題を提起しようとします。

25年以上のプログラミング、私はかなり多くの異なる概念を見つけました、私は何かを試すことができました、私はまだ時間がありませんでした。 現在、Go言語を興味を持って見ています。これは、「Wirth言語の系列」の後継者であるAlgol-Pascal-Modula-Oberonに起因するものです。 このチェーンの優れた機能の1つは、後続の各言語が以前の言語よりも単純になることですが、それほど強力ではなく表現力も豊かになります。

単純な言語が良い理由は誰もが理解していると思います。 しかし、まだポップアップ表示されるので、これらの基準を提供します。



では、なぜ複雑な言語なのでしょうか? それはすべて表現力についてです。 設計によって必要なアクションを簡単に説明できる場合は、言語の複雑さのマイナス面を十分に取り戻すことができます。

比較的短い期間、C#言語は、その設計に反映されている非常に多くの異なる概念を吸収しました。 それらの追加の速度は時々怖いです。 私には、最初からほとんどC#を使用しているので、簡単です。 しかし、始めたばかりの初心者はどうでしょうか? Javaプログラマーがうらやましいこともあります。Javaプログラマーは、より保守的に言語に導入されています。

言語に追加されたもの-あなたは本当にxでそれを切り取ることができません。 もちろん、狭い円に広がっている言語を使用する場合、バージョン間の非互換性を確保できます。 後方非互換性の「いたずら」は、Pythonなどの言語によって実現できます(2番目から3番目のバージョンに切り替える場合)。 しかし、Microsoftの背後にあるC#ではありません。 開発者が、新しい機能ごとに言語が(特定の場合に)より便利になるだけでなく、「肥満」による死に少し近づいていることを理解した場合、コメントはこれが行われるよりも少し熱心ではないようですC#7のイノベーションへの最初の対応ブランチ。

以下に説明するのは、これが本当に有用なものであるかどうかについての私の推測です。 もちろん、これは好みの問題であり、誰もが私に同意するわけではありません(ネタバレ参照)。 そして、いずれにせよ、これはC#に永遠に残るでしょう...まあ、少なくとも特異点の瞬間までは。

バージョンごとに追加された言語機能のリストは、 C#バージョンで追加された機能にあります 。 バージョン2.0には触れず、3.0から始めます。

叙情的な思い出
言語のほぼすべての現象、事実、特徴には、プラス面とマイナス面の両方があります。 これに与える評価は、主観的な特性に大きく依存し、他の人は反対の方法で同じことを評価できます。 これは自然なことであるだけでなく、ほとんどの場合、これらの推定値はどちらの場合も正しいでしょう。 異なる人々のためだけに。 ネタバレでは、そのような違いの例を示してみます。


C#3.0


暗黙的に型指定されたローカル変数


var i = 5;
var s = "Hello";
var d = 1.0;
var numbers = new int[] {1, 2, 3};
var orders = new Dictionary<int,Order>();

var. Java («Var val Java?», « «var» Java: , »)

, ( ). , , . , . , , , JavaScript-, .

:

List<Pair<String, Double>> scores = seeker.getScores(documentAsCentroid);
...
foreach(Pair<String, Double> score in scores)


(Pair<String, Double>) , . – ( , ). . Java, C#, Type (typedef C). C# using, - :

using StopWordsTables = System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, string>>;


, , StopWordsTables. , …

typedef, .

, var , (.., ) . Code Agreement ( ). . Var . .

— var . . , , , , . Type/typedef .

– var, , Go? “=” “:=”, , . . … , type Go , .

— var C# . typedef.

var , , - .

Object and collection initializers


var r = new Rectangle {
   P1 = new Point { X = 0, Y = 1 },
   P2 = new Point { X = 2, Y = 3 }
};
List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

, . .

Auto-Implemented properties


,
public Class Point {
   private int x;
   private int y;
   public int X { get { return x; } set { x = value; } }
   public int Y { get { return y; } set { y = value; } }
}

:
public Class Point {
   public int X { get; set; }
   public int Y { get; set; }
}

, ? , Java , . , ( ) .

Anonymous types


var p1 = new { Name = "Lawnmower", Price = 495.00 };
var p2 = new { Name = "Shovel", Price = 26.95 };
p1 = p2;

. , 1 , . . , , . , , ( , JSON, , ).

Extension methods


namespace Acme.Utilities
{
   public static class Extensions
   {
      public static int ToInt32(this string s) {
         return Int32.Parse(s);
      }
      public static T[] Slice<T>(this T[] source, int index, int count) {
         if (index < 0 || count < 0 || source.Length – index < count)
            throw new ArgumentException();
         T[] result = new T[count];
         Array.Copy(source, index, result, 0, count);
         return result;
      }
   }
}


using Acme.Utilities;
...
string s = "1234";
int i = s.ToInt32();               // Same as Extensions.ToInt32(s)
int[] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] a = digits.Slice(4, 3);      // Same as Extensions.Slice(digits, 4, 3)

. , () .

Query expressions


LINQ. ! , , - . , LINQ , . ? - (), SQL-, .

string[] people = new [] { "Tom", "Dick", "Harry" };
//     
var filteredPeople = from p in people where p.Length > 3 select p; 
//     
var filteredPeople = people.Where (p => p.Length > 3); 


:



WPF. , XML. . , — , XML? . , — , . .
, LINQ — , .

Lambda expressions


x => x + 1                     // Implicitly typed, expression body
x => { return x + 1; }         // Implicitly typed, statement body
(int x) => x + 1               // Explicitly typed, expression body
(int x) => { return x + 1; }   // Explicitly typed, statement body
(x, y) => x * y               // Multiple parameters
() => Console.WriteLine()      // No parameters

, C# , . , . .

Expression trees


LINQ Lambda.

Partial methods


. — .

,
-61, , . , , , . 4 ( ) (, ) , , «» .

— , , « » 0010 . , 18011 , , (16 ). ? 8 , 8 . / . , .

, 1502. IBM-PC , , MS DOS, . , .

, . . , . , — . 1502 (x86) ( ), 0010 (PDP-11). . , . — , DATA, . ! — . .

, , ( ), . , , ( — ). , , — . , ! , , — - , , ( — 25 ).

- , , -, — . ( ). — .


C# 4.0


Dynamic binding



var doc = new XmlDocument("/path/to/file.xml");
var baz = doc.GetElement("foo").GetElement("qux");


dynamic doc = new XmlDocument("/path/to/file.xml");
var baz = doc.foo.qux;

, , . dynamic — , . — . , , . , (, ). , , . — - , . . . , . , , .

Named and optional arguments


class Car {
  public void Accelerate(
    double speed, int? gear = null, 
    bool inReverse = false) { 
    /* ... */ 
  }
}

Car myCar = new Car();
myCar.Accelerate(55);

— ( , ). .

Generic co- and contravariance


. . .

Embedded interop types («NoPIA»)


, , — , . , COM , () MS Office, .

C# 5.0


Asynchronous methods


public async Task ReadFirstBytesAsync(string filePath1, string filePath2)
{
    using (FileStream fs1 = new FileStream(filePath1, FileMode.Open))
    using (FileStream fs2 = new FileStream(filePath2, FileMode.Open))
    {
        await fs1.ReadAsync(new byte[1], 0, 1); // 1
        await fs2.ReadAsync(new byte[1], 0, 1); // 2
    }
}

. , — (Async/await C#: ). (Await in catch/finally blocks) , (akka.net , ). - — . . , .

Caller info attributes


public void DoProcessing()
{
    TraceMessage("Something happened.");
}

public void TraceMessage(string message,
        [System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
        [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
        [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
    System.Diagnostics.Trace.WriteLine("message: " + message);
    System.Diagnostics.Trace.WriteLine("member name: " + memberName);
    System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
    System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);
}

// Sample Output:
//  message: Something happened.
//  member name: DoProcessing
//  source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs
//  source line number: 31

, , . .

C# 6.0


Compiler-as-a-service (Roslyn)


( ) . , .

Import of static type members into namespace


using static System.Console;
using static System.Math;
using static System.DayOfWeek;
class Program
{
    static void Main()
    {
        WriteLine(Sqrt(3*3 + 4*4)); 
        WriteLine(Friday - Monday); 
    }
}

. , , . — . , . , Intellisense , . , , — .

Exception filters


try { … }
catch (MyException e) when (myfilter(e))
{
    …
}

. , ? , , ?

Await in catch/finally blocks


— .

Auto property initializers


public class Customer
{
    public string First { get; set; } = "Jane";
    public string Last { get; set; } = "Doe";
}

. , .

Default values for getter-only properties


public class Customer
{
    public string First { get; } = "Jane";
    public string Last { get; } = "Doe";
}

.

Expression-bodied members


public string Name => First + " " + Last;
public Customer this[long id] => store.LookupCustomer(id); 

, . , . — .

Null propagator (succinct null checking)


public static string Truncate(string value, int length)
{          
  return value?.Substring(0, Math.Min(value.Length, length));
}

. . , , , .

String Interpolation


! , -, . :

“Total lines: “ + totalLines + ”, total words: “ + totalWords + ”.”;


, ? , , 2 :


, Format(...) .
. , , C# :) !

nameof operator


if (x == null) throw new ArgumentNullException(nameof(x));
WriteLine(nameof(person.Address.ZipCode)); // prints "ZipCode"

“Caller info attributes”. .

Dictionary initializer


var numbers = new Dictionary<int, string> {
    [7] = "seven",
    [9] = "nine",
    [13] = "thirteen"
};

, , . . .

-
, . . , . , . , . -, . , . , — . , IDE — .

Java. ( )

, http- ( ) . , . , , , . , . , . . , .

, — . , ( — ) . , , , . , . — , .


C# 7.0 proposals


— , . . “ C# 7”, .

Binary literals


int x = 0b1010000; 
int SeventyFive = 0B10_01011;

, , , — . « » — ?

Local functions


public void Foo(int z)
{
    void Init()
    {
        Boo = z;
    }
    Init();
}

C# (Delphi), , . . , , C# — . . , , . , .

, — . , ( , , ), .

Tuples


, , / out-. .

Pattern matching, conditions in switch


// type pattern
public void Foo(object item)
{
    if (item is string s)
    {
        WriteLine(s.Length);
    }
}
// Var Pattern
public void Foo(object item)
{
    if(item is var x)
    {
        WriteLine(item == x); // prints true
    }
}
// Wildcard Pattern
public void Foo(object item)
{
    if(item is *)
    {
        WriteLine("Hi there"); //will be executed
    }
}

.
, , . , , . .

Ref locals and ref returns


static void Main()
{
    var arr = new[] { 1, 2, 3, 4 };
    ref int Get(int[] array, int index)=> ref array[index]; 
    ref int item = ref Get(arr, 1);
    WriteLine(item);
    item = 10;
    WriteLine(arr[1]);
    ReadLine();
}
// Will print
// 2
// 10

. .

“” “ ” 7- C#, .

, ?


, C# (7- , ) :

  1. - LINQ. fluent-.
  2. .
  3. Var. , .
  4. — .


:

  1. LINQ ( ).
  2. .
  3. , , .
  4. .
  5. Async/await.
  6. .

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


All Articles