C#6.0の新機能紹介
-
Upload
kazunori-hamamoto -
Category
Environment
-
view
745 -
download
0
description
Transcript of C#6.0の新機能紹介
C# 6.0 のご紹介
Japan Windows Azure User Group福岡支部
濱本 一慶 (@airish9)
2
自己紹介濱本一慶(はまもと かずのり)@airish9
C# と VB.NET でご飯を食べてます!
エンタープライズ系の開発中心
Azure で好きなサービスはモバイルサービス
お仕事では C# と VB.NET を半々くらい
3
アジェンダ
C# の今まで
C# のこれから
C# 6.0 の新機能
C# 6.0 の体験方法
4
C# の今まで
C#1.02000 年
C#2.02004 年
C#3.02007 年
C#4.02008 年
C#5.02012 年
C#6.020XX 年
クラス
ジェネリクス
ラムダ式LINQ
dynamic
await/async
Roslyn
5
注目度は…
Google 先生が言うには Java が断トツ人気・・・。
6
C# のこれから
2014 年 4 月
C# にとって大きな出来事が…
7
C# のこれから
Roslyn がオープンソースになりました!!
http://roslyn.codeplex.com/
8
Roslyn って何?
.NET Framework の次世代コンパイラプラットフォーム
Compiler APIs
• コンパイラ
Feature APIs
• Code Fix やリファクタリング等
Workspace APIs
• プロジェクトファイルや参照関係
9
C# 6.0
Visual Studio 14 で使用可能(現在は CTP3)
実は VisulaBasic も便利になります
Roslyn ≒ C#6.0
10
C#6.0 の実装状況
http://roslyn.codeplex.com/wikipage?title=Language%20feature%20status&referringTitle=Home
Exists できてた
Done できた
Planned やってます
Maybe 多分する
Withdrawn やりたい
No 今は無理
N/A 必要なし
要点 例 進捗どうですか
Primary constructors class Point(int x, int y) { … } Done
Auto-property initializers public int X { get; set; } = x; Done
Getter-only auto-properties public int Y { get; } = y; Done
Using static members using System.Console; … Write(4); Done
Dictionary initializer new JObject { ["x"] = 3, ["y"] = 7 } Done
Indexed member initializer new JObject { $x = 3, $y = 7 } Withdrawn
Indexed member access c.$name = c.$first + " " + c.$last; Withdrawn
Declaration expressions int.TryParse(s, out var x); Done
Await in catch/finally try … catch { await … } finally { await … } Done
Exception filters catch(E e) if (e.Count > 5) { … } Done
11
C#6.0 の実装状況要点 例 進捗どうですかTypecase Select Case o : Case s As String : … No Guarded cases Select Case i : Case Is > 0 When i Mod 2 = 0 No Partial modules Partial Module M1 N/A Partial interfaces Partial Interface I1 Exists Multiline string literals "Hello<newline>World" Exists Year-first date literals Dim d = #2014-04-03# N/A Binary literals 0b00000100 Planned Digit separators 0xEF_FF_00_A0 Planned Line continuation comments Dim addrs = From c in Customers ' comment N/A TypeOf IsNot If TypeOf x IsNot Customer Then … N/A Expression-bodied members public double Dist => Sqrt(X * X + Y * Y); Planned Event initializers new Customer { Notify += MyHandler }; Planned Null propagation customer?.Orders?[5]?.$price Done Semicolon operator (var x = Foo(); Write(x); x * x) Maybe Private protected private protected string GetId() { … } Withdrawn
Params IEnumerable int Avg(params IEnumerable<int> numbers) { … }
Planned
Constructor Inference new Tuple(3, "three", true); Maybe String interpolation "\{p.First} \{p.Last} is \{p.Age} years old." Maybe TryCast for nullable Dim x = TryCast(u, Integer?) Exists Delegate combination with + d1 += d2 Exists
12
C#6.0 の実装状況要点 例 進捗どうですか
Implicit implementation Class C : Implicitly Implements I Exists
nameof operator string s = nameof(Console.Write); Exists
Strict modules Strict Module M Exists
Faster CInt Dim x = CInt(Math.Truncate(d)) | Exists
#pragma #Disable Warning BC40008 Exists
Checked and Unchecked blocks Checked : x += 1 : End Checked Exists
Field targets on autoprops <Field: Serializable> Property p As Integer Planned
If(b,e1,e2) uses type context Dim x As Integer? = If(b,1,Nothing) N/A
13
C#6.0 の実装状況要点 例
Primary constructors class Point(int x, int y) { … }
Auto-property initializers public int X { get; set; } = x;
Getter-only auto-properties public int Y { get; } = y;
Using static members using System.Console; … Write(4);
Dictionary initializer new JObject { ["x"] = 3, ["y"] = 7 }
Declaration expressions int.TryParse(s, out var x);
Await in catch/finally try … catch { await … } finally { await … }
Exception filters catch(E e) if (e.Count > 5) { … }
Partial interfaces Partial Interface I1
Multiline string literals "Hello<newline>World"
Null propagation customer?.Orders?[5]?.$price
TryCast for nullable Dim x = TryCast(u, Integer?)
Delegate combination with + d1 += d2
Implicit implementation Class C : Implicitly Implements I
nameof operator string s = nameof(Console.Write);
Strict modules Strict Module M
Faster CInt Dim x = CInt(Math.Truncate(d)) |
#pragma #Disable Warning BC40008
Checked and Unchecked blocks Checked : x += 1 : End Checked
19 機能が実装済
14
Auto-property initializers 自動実装プロパティの初期化がコンストラクタで可能になりました!
class Person{ public int Age { get;set; }
public Person() { Age = 20; }}
Before
class Person{ public int Age { get;set; } = 20;}
Next
15
Getter-only auto-propertiesGetter のみの自動実装プロパティの初期化もできます
class Person{ public int Age { get { return this._age; } } private int _age = 20;}
Before
class Person{ public int Age { get; } = 20;}
Next
16
Primary constructors クラス定義と同時にコンストラクタを宣言する
class Person{ public string Name { get;set; } public Person(string name) { this.Name = name; }}
Before
class Person(string name){ public string Name { get;set; } = name;}
Next
17
Primary constructors コンストラクタと同じように処理も記述できます
class Person{ public string Name { get; set; }
public Person(string name) { if (string.IsNullOrEmpty(name)) throw new ArgumentException("name is NullOrEmpty"); this.Name = name; }}
Before
18
Primary constructors コンストラクタと同じように処理も記述できます
class PersonNext(string name){ { if (string.IsNullOrEmpty(name)) throw new ArgumentException("name is NullOrEmpty"); }
public string Name { get;set; } = name;}
After
19
Using static members静的クラスの静的メソッドは省略可能になります
class Person{ public void Say() { System.Console.WriteLine("Name is " + this.Name); }}
Before
using System.Console;
class Person{ public void Say() { Console.WriteLine("Name is " + this.Name); }}
After
20
Declaration expressionsParse する時に無駄な変数を宣言しなくてよくなります。
public void Say(){ int number; if (int.TryParse("20", out number)) { System.Console.WriteLine(number + " 歳です "); }}
Before
public void Say(){ if (int.TryParse("20", out var number)) { Console.WriteLine(number + " 歳です "); } Console.WriteLine(number + “ 歳です” ); // スコープ外なのでエラー}
After
21
Await in catch/finallyCatch 句と Finally 句で await が使用できようになりました
try{ //HodeHoge}catch (Exception e){ await Task.Delay(2000);}
After
Exception exception = null;try{ //HodeHoge}catch (Exception e){ exception = e;}if (exception != null) await Task.Delay(2000);
Before
22
Exception filtersCatch 句に条件式を付与する事ができるようになりました
try{ //HogeHoge}catch (Exception e) if (e.Data.Count > 10){ // 例外処理}
After
try{ //HogeHoge}catch (Exception e) { if (e.Data.Count > 10) { // 例外処理 }}
Before
23
Null propagation null チェックをシンプルに記述できます
public int? GetNameLength(Person person){ if (person == null) return null; if (person.Name == null) return null; return person.Name.Length;}
Before
public int? GetNameLength(Person person){ return person?.Name?.Length;}
After
24
nameof operatorメンバや変数の名前を返す演算子
class Person{ public string Name { get; set; }
public Person(string name) { if (string.IsNullOrEmpty(name)) throw new ArgumentException("name" + " is NullOrEmpty"); this.Name = name; }}
Before
文字列指定
25
nameof operatorメンバや変数の名前を返す演算子
public string Name { get; set; }
public Person(string name){ if (string.IsNullOrEmpty(name)) throw new ArgumentException(nameof(name) + "is NullOrEmpty"); this.Name = name;}
After
nameof 演算子でタイプセーフ
26
参考元Roslyn について情報が整理されているサイト
++C++; // 未確認飛行 C ブログ
http://ufcpp.wordpress.com
xin9le.net
http://xin9le.net/
27
Roslyn(C#6.0) を使うには
1. Visual Studio 14 CTP をインストール
2. プロジェクトファイルを編集
http://www.visualstudio.com/en-us/downloads/visual-studio-14-ctp-vs
次ページにて詳細を説明
しかし、 2014 年 9 月時点では VisualStudio 14 CTP はside-by-side できない・・・
28
Roslyn(C#6.0) を使うにはプロジェクトファイルの編集箇所
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PlatformTarget>AnyCPU</PlatformTarget> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <LangVersion>experimental</LangVersion> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PlatformTarget>AnyCPU</PlatformTarget> <DebugType>pdbonly</DebugType> <Optimize>true</Optimize> <OutputPath>bin\Release\</OutputPath> <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <LangVersion>experimental</LangVersion> </PropertyGroup>
追加
追加
29
もっと簡単に Roslyn を体験するには…
30
もっと簡単に Roslyn を体験するには…
Azure の仮想マシンを作成
ギャラリーから VisualStudio 14 CTP を選択
31
お知らせ
C#er が集まって、ゆるーく hogehoge やってます
C# に限らず Microsoft 系の開発の集まりです
「 C# 勉強会福岡」を不定期で開催中
興味がある方は Facebook ページからどうぞ
32
ご清聴ありがとうございました!
Let’s play C# !!