The Future of C#ã®ã¾ã¨ã
The Future of C# | pdc2008 | Channel 9
VS2010ããã¦ã³ãã¼ãã§ããªãã®ã§ãä¸è²è
ãã¦PDC2008ã®ãã¸ããã»ãã·ã§ã³ãã¾ã¨ãã¦ã¿ã¾ããããã¢ãªã³ã°ãå£æ»
çã«è¦æãªã®ã§ããã¾ãå½ã¦ã«ãªããªãã¨æãã¾ãã»ã»ã»(^^;
C#ã®é²å
C#ã¯ããã¼ã¸ã³ã¼ãããå§ã¾ã£ã¦ã¸ã§ããªãã¯ãLINQã¨é²åãã¦ããã
ãã¬ã³ã
- Declarative
- Dynamic
- Concurrent
宣è¨å(Declarative)
LINQã§é¢æ°åè¨èªã®è¦ç´ ãåãå
¥ãã¦ããã
å½ä»¤å(Imperative)ã¯How(ã©ã®ããã«)ã«éç¹ãç½®ãã¦ãããã
宣è¨åã¯What(ä½ã)ã«éç¹ãç½®ãã¦ããã
ãã¼ãã¦ã§ã¢ã®é²æ©ã«ãããHowãJITçã«ä»»ããããããã«ãªãã®ã§ãä½ãããããéè¦ã«ãªã£ã¦ããã
éç(Static)ã¨åç(Dynamic)
éç
- é ä¸
- ãã¤ããã©ã¼ãã³ã¹
- ã¤ã³ããªã¸ã§ã³ã¹ãªãã¼ã«ã®ãµãã¼ã
- ããè¯ãã¹ã±ã¼ãªã³ã°
åç
- åç´ãã¤ç°¡æ½
- æé»çãªå
- ã¡ã¿ããã°ã©ãã³ã°
- ã³ã³ãã¤ã«ä¸è¦
並å(Cuncurrent)
ãã«ãã³ã¢CPUã®æ®åãè¨èªãã©ã¤ãã©ãªã«ãããµãã¼ãã®å¿
è¦æ§ã
å
±é²å(Co-Evolution)
C#ã¯ãã«ããã©ãã¤ã è¨èª(OOPãFPãªã©)ã¨ãã¦é²åãã¦ãããã
ãã¹ã¦ãC#ã§è³ãã®ã§ã¯ãªããä»ã®è¨èªãå¾æãªåéã¯ãã®è¨èªã«ä»»ãã¦ãå調ããã®ããã¹ãã
C#4.0 Dynamic Programming
C#4.0ã§ã¯åçããã°ã©ãã³ã°ã®ãµãã¼ããå¼·åãããããã¯å
ã«è¿°ã¹ãä»è¨èªã¨ã®å調ããã¤ã³ãã¨ãªãã
C#4.0ã®æ°æ©è½
- åçã¿ã¤ã
- çç¥ãã©ã¡ã¼ã¿ã¨ååä»ããã©ã¡ã¼ã¿
- COMç¸äºéç¨ã®æ¹å
- å ±å¤æ§ã¨åå¤æ§
.NETã«ãããåçããã°ã©ãã³ã°
å
±éåºç¤ã¨ãã¦ã®DLRã
- Expression Trees
- Dynamic Dispatch
- Call Site Chaching
CLRã¯éçè¨èªã«å¯¾ããå ±éåºç¤ã§ãã£ããããããè£å®ããããã«DLRãã§ããã
以éãC#4.0ã®æ°æ©è½ç´¹ä»ã
åçã¿ã¤ã
// å¤ãè¯ãéçåããã°ã©ãã³ã° Calculator calc = GetCalculator(); int sum = calc.Add(10, 20);
ä¸æ¹ã»ã»ã»
// COM or ruby or python ro javascript or ... object calc = GetCalculartor(); Type calcType = calc.GetType(); object res = calcType.InvokeMember("Add", new object[] {10, 20}); int sum = Converter.ToInt32(res);
ããã¯å¤§å¤ã
// ScriptObjectãªã ScriptObject calc = GetCalculator(); object res = calc.Invoke("Add", 10, 20); int sum = Converter.ToInt32(res);
ã¡ãã£ã¨æ¥½ã
// C# 4.0ã¹ã¿ã¤ã« dynamic calc = GetCalculator(); int sum = calc.Add(10, 20);
dynamicãã¼ã¯ã¼ãã«ããã¡ã½ããã®åçå¼ã³åºã(å®è¡æ解決)ï¼æ»ãå¤ã®èªåå¤æãã¯ãªã
C#4.0ã§åçã¿ã¤ãããµãã¼ãããããéçã¿ã¤ãã¸ã®èããå¤ãã£ãããã§ã¯ãªãã
éçã¿ã¤ãã¯ä¾ç¶è¯ããã®ã§ãããåçã¿ã¤ãã®ãµãã¼ãã¯å調ãã¹ãéè¦ãªåçè¨èª(Javascript/ruby/python...)ã®å¢å ã«ãããã®ã§ããã
çç¥å¯è½ï¼ååä»ããã©ã¡ã¼ã¿
// çç¥å¯è½ãã©ã¡ã¼ã¿ã®ä¾ public StreamReader OpenTextFile( string path, Encoding encoding = null, bool detectEncoding = true, int bufferSize = 1024); // ååä»ããã©ã¡ã¼ã¿(å¼ã³åºã) OpenTextFile("foo.txt", Encoding.UTF, bufferSize:4096);
注æ ååä»ããã©ã¡ã¼ã¿ã¯æå¾ã«ç½®ãå¿ è¦ãããã
ãããã¯VBã¨ã®ç¸äºéç¨ã®ããã«å°å ¥ãããã®ã§æ¿«ç¨ã¯ç¦ç©ã
COMç¸äºéç¨ã®æ¹å
object fileName = "Test.docx"; object missing = Sytem.Reflection.Missing.Value; doc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
大éã®missingã»ã»ã»
C#4.0ã§ã¯ã
doc.SaveAs("Test.docx");
ããã ãã§OKãCOMå¼ã³åºãã«ãããrefãã©ã¡ã¼ã¿ã¯çç¥å¯è½ã¨ãªãã
- ãªã¼ãã¡ã¼ã·ã§ã³ãªãã¸ã§ã¯ã -> dynamicã¸ãããã³ã°
- çç¥å¯è½ï¼ååä»ããã©ã¡ã¼ã¿ã«ããç¸äºéç¨æ§åä¸
- çç¥å¯è½ref
- No PIA
å
±å¤æ§ã¨åå¤æ§
å
±å¤æ§ã®åé¡ç¹ã
string strings = GetStringArray(); // co-variant
Process(strings);
// Processã¡ã½ããã®å¼æ°ã¯objectã ãã渡ãã
// ããããobjectsã®å®æ
ã¯stringã ããã¡ã½ããå
ã§Buttonã代å
¥
void Process(object[] objects)
{
objects[0] = "Hello"; // OK
objects[1] = new Button(); // Exception!
}
åå¤æ§ã®åé¡ç¹ã
List<string> strings = GetStringList(); Process(strings); // ã³ã³ãã¤ã«ã¨ã©ã¼! // IEnumerable<T>ã¯åå¤æ§ãªã®ã§IEnumerable<string>ã¯æ¸¡ããªã void Process(IEnumerable<object> objects) {...}
ããããIEnumerableã¯èªã¿åãå°ç¨ãªã®ã§ã渡ãã¦ãåé¡ã¯ãªãã¯ãã»ã»ã»
ããã§ã
// out = Co-variant output positions only public interface IEnumerable<out T> {} public interface IEnumerator<out T> {}
outãã¼ã¯ã¼ããä»ãããã¨ã§å
±å¤æ§ã表ãã
ããã§ã
List<string> strings = GetStringList(); Process(strings); // OK.
ã³ã³ãã¤ã«åºæ¥ãã
ãã¡ãã¯ãåå¤æ§ã®æå®ã
// in public interface IComparer<in T> { int Compare(T x, T y); }
C#4.0ã®å¯å¤æ§ã®ã¾ã¨ã
- ã¤ã³ã¿ãã§ã¼ã¹ã¨ããªã²ã¼ãã«ã¤ãã¦ã¯ãµãã¼ããã¦ãã
- in/outãã¼ã¯ã¼ãã«ãããã§ãã¯ãå¯è½
- å¤åã«å¯¾ãã¦ã¯å¸¸ã«åå¤æ§
- ref/outãã©ã¡ã¼ã¿ã¯åå¤æ§ã¨ãªã
å人çãªææ³ã
C#1.0ãåºãæã«ãP/Invokeã¨unsafeã«ä»£è¡¨ãããããã«Win32API/C/C++ã¨ã®ç¸äºéç¨ããã£ã¡ãæ¼ããã¦ãã¾ããããä»åã¯åºãããè¦ãã¦ããJavaScriptãRubyãPythonãªã©ã¨ã®ç¸äºéç¨ãå¼·åãã¦ãã¦ããã¤ãã£ãã³ã¼ããã¹ã¯ãªããè¨èªã¾ã§çµã³ã¤ããå¼·åãªç³è¨èªã«ãªãã¾ããããè¨èªæ©è½æ¬ä½ã¨ãã¦ã¯ãçãã¨ããã«æãå±ãããç¨åº¦ã«æãã¾ãããC#ã®ãã¸ã·ã§ã³çã«ã¯ãããããããããã®æ©è½å¼·åã§è¯ãã®ããç¥ãã¾ãããã