ã€ã³ã¿ãã¥ãŒã«åæ Œ/å®æœãããšã.NETãã©ã®ããã«æ©èœãããã«ã€ããŠã®äžè¬çãªç解ãæããã«ãã質åã«åºäŒããŸãã ç§ã®æèŠã§ã¯ããã¬ããŒãžã³ã¬ã¯ã¿ãŒãã®ä»äºã«ã€ããŠã®è³ªåã¯ãã®ãããªè³ªåã®äžã§æãæãããŠããŸãããäžåºŠã¹ããªã³ã°ã€ã³ã¿ãŒã³ã«ã€ããŠè³ªåãããããšããããŸãã ãããŠåœŒã¯æ£çŽã«ç§ãè¡ãæ¢ãŸãã«ããã Runetã§ã®æ€çŽ¢ã§ã¯ããã€ãã®èšäºãè¿ãããŸããããç§ãæ¢ããŠãã質åã«å¯Ÿããåçã¯ãããŸããã§ããã Andrew StellmanïŒ ãHead First CïŒãã®èè ïŒ ã«ããèšäºã®ç¿»èš³ããã®ã®ã£ãããåããããšãé¡ã£ãŠããŸãã ãã®è³æã¯ãåå¿è ã.NETéçºè ãããã³.NETã®æååã®æçãšã¯äœãã«èå³ãæã€äººã«ãšã£ãŠæçšã ãšæããŸãã
String.Internã¯æååãããã«é¢çœãããŸã
ãã¹ãŠã®CïŒåå¿è éçºè ãæåã«ééããããšã®1ã€ã¯ãæååã®æäœã§ãã ä»ã®ã»ãšãã©ãã¹ãŠã®CïŒæ¬ã§è¡ãããŠããããã«ããHead First CïŒãã®åé ã§æååãæäœããåºæ¬ã瀺ããŸãã ãããã£ãŠãCïŒã®ãžã¥ãã¢ããã³ããã«ã¬ãã«ã®éçºè ããããªãè¯ãã©ã€ã³ããšã®ããŒã¹ãæã«å ¥ãããšæããã®ã¯é©ãã¹ãããšã§ã¯ãããŸããã ããããç·ã¯èŠãç®ãããèå³æ·±ããã®ã§ãã CïŒããã³.NETã®æååã®æãèå³æ·±ãåŽé¢ã®1ã€ã¯ãString.Internã¡ãœããã§ãã ãã®æ¹æ³ã®ä»çµã¿ãç解ãããšãCïŒéçºã®ã¹ãã«ãåäžãããããšãã§ããŸãã ãã®æçš¿ã§ã¯ãString.Internã¡ãœããã®ç°¡åãªãã¥ãŒããªã¢ã«ãäœæããŠããã®ä»çµã¿ã説æããŸãã
泚ïŒãã®æçš¿ã®æåŸã§ãILDasmã䜿çšããŠãå éšãã衚瀺ããŸãã ILDasmã䜿çšããããšããªãå Žåãããã¯éåžžã«äŸ¿å©ãª.NETããŒã«ãç¥ãè¯ãæ©äŒã«ãªããŸãã
æååã®æäœã®åºæ¬
System.Stringã¯ã©ã¹ãæåŸ ãããã®ã®ç°¡åãªæŠèŠããå§ããŸãããã ïŒè©³çŽ°ã«ã€ããŠã¯è§ŠããŸããã誰ãã.NETã®æååã®åºæ¬ã«ã€ããŠæçš¿ãããå Žåã¯ãã³ã¡ã³ããè¿œå ãããã Building Better Softwareã«é£çµ¡ããŠãã ãããå¯èœãªèšäºã«ã€ããŠäžç·ã«è©±ãåãããšãã§ããŸãïŒïŒ
Visual Studioã§æ°ããã³ã³ãœãŒã«ã¢ããªã±ãŒã·ã§ã³ãäœæããŸãã ïŒcsc.exeã䜿çšããŠã³ãŒããã³ã³ãã€ã«ããå Žåã¯ãã³ãã³ãã©ã€ã³ãããã¹ãŠåãããã«æ©èœããŸããããããªã¢ã«ã®èªèã容æã«ããããã«ãVisual Studioã§ã®éçºã«åºå·ããŸããããïŒæ¬¡ã«ã MainïŒïŒã¡ãœããã®ã³ãŒãã瀺ããŸã-ã³ã³ãœãŒã«ã¢ããªã±ãŒã·ã§ã³ã®ãšã³ããªãã€ã³ãïŒ
Program.csïŒ
using System; class Program { static void Main(string[] args) { string a = "hello world"; string b = a; a = "hello"; Console.WriteLine("{0}, {1}", a, b); Console.WriteLine(a == b); Console.WriteLine(object.ReferenceEquals(a, b)); } }
ãã®ã³ãŒãã«é©ãã¯ãªãã¯ãã§ãã ããã°ã©ã ã¯ã³ã³ãœãŒã«ã«3è¡ã衚瀺ããŸãïŒVisual Studioã§äœæ¥ããŠããå Žåã¯ãCtrl-F5ã䜿çšããŠãããã¬ãŒã®å€éšã§ããã°ã©ã ãå®è¡ããŸãããŸããã³ã³ãœãŒã«ãŠã£ã³ããŠãéããªãããã«ã ãPress any key ...ããããã°ã©ã ã«è¿œå ãããŸãïŒïŒ
ããã«ã¡ã¯ãããã«ã¡ã¯
åœ
åœ
æåã®WriteLineïŒïŒã¯ 2è¡ãåºåããŸãã 2çªç®ã¯ãæååãäžèŽããªãããFalseãè¿ãçå€æŒç®å==ã䜿çšããŠããããæ¯èŒããŸãã åŸè ã¯ããããæ¯èŒããŠãäž¡æ¹ã®å€æ°ãåãStringãªããžã§ã¯ããåç §ããŠãããã©ããã確èªããŸãã ããã§ã¯ãªããããã¡ãœããã¯å€Falseã衚瀺ããŸãã
次ã«ããããã®2è¡ãMainïŒïŒã¡ãœããã®æåŸã«è¿œå ããŸãã
Console.WriteLine((a + " world") == b); Console.WriteLine(object.ReferenceEquals((a + " world"), b));
ãããŠåã³ãããªãã¯ããªãæçœãªçããåŸãã äž¡æ¹ã®æååãçãããããçå€æŒç®åã¯Trueãè¿ããŸãã ãã ãããHelloãããã³ãworldãæååã®é£çµã䜿çšããå Žåã +æŒç®åã¯ããããé£çµãã System.Stringã®æ°ããã€ã³ã¹ã¿ã³ã¹ãè¿ããŸã ã ãã®ããã object.ReferenceEqualsïŒïŒã¯ããªãåççã«Falseãè¿ããŸãã ReferenceEqualsïŒïŒã¡ãœããã¯ãäž¡æ¹ã®åŒæ°ãåããªããžã§ã¯ããåç §ããå Žåã«ã®ã¿Trueãè¿ããŸãã
ãã®ã¡ãœããã䜿çšãããšããªããžã§ã¯ããéåžžã©ããæäœã§ããŸãã 2ã€ã®ç°ãªããªããžã§ã¯ãã«åãå€ãèšå®ã§ããŸãã ãã®åäœã¯éåžžã«å®çšçã§äºæž¬å¯èœã§ãã 2ã€ã®ãããŒã ããªããžã§ã¯ããäœæãããã¹ãŠã®ããããã£ãåãå€ã«èšå®ãããšã2ã€ã®åäžã®ãããŒã ããªããžã§ã¯ããäœæãããŸããããããã¯ç°ãªããªããžã§ã¯ãã«ãªããŸãã
ããã¯ãŸã å°ãæ··ä¹±ããŠããããã«èŠããŸããïŒ ãããããªããç§ã¯ééããªãâ Head First CïŒâã®æåã®ããã€ãã®ç« ã«æ³šæãæãããšããå§ãããŸãã ãã®æ¬ããç¡æã®åãæããšããŠããŠã³ããŒãã§ããŸãã
ãããã£ãŠãæååã䜿çšããŠããéã¯ããã¹ãŠåé¡ãããŸããã ããããè¡åç §ã§éãã§ãããšããã«ãå°ãå¥åŠã«ãªããŸãã
ãã®ãªã³ã¯ã«åé¡ããããŸã...
æ°ããã³ã³ãœãŒã«ã¢ããªã±ãŒã·ã§ã³ãäœæããŸãã 以äžã®ã³ãŒãã¯åœŒã®ããã§ãã ããããã³ã³ãã€ã«ããŠå®è¡ããåã«ãã³ãŒãã泚ææ·±ãèŠãŠãã ããã ã³ã³ãœãŒã«ã«è¡šç€ºããããã®ãæšæž¬ããŠã¿ãŠãã ããïŒ
Program.csïŒ
using System; class Program { static void Main(string[] args) { string hello = "hello"; string helloWorld = "hello world"; string helloWorld2 = hello + " world"; Console.WriteLine("{0}, {1}: {2}, {3}", helloWorld, helloWorld2, helloWorld == helloWorld2, object.ReferenceEquals(helloWorld, helloWorld2)); } }
次ã«ããã°ã©ã ãå®è¡ããŸãã ã³ã³ãœãŒã«ã«è¡šç€ºããããã®ã¯æ¬¡ã®ãšããã§ãã
ããã«ã¡ã¯äžçãããã«ã¡ã¯äžçïŒTrueãFalse
ãããŠãããã¯ãŸãã«ç§ãã¡ãæåŸ ãããã®ã§ãã helloWorldããã³helloWorld2ãªããžã§ã¯ãã§ã¯ãæååã«ãHello worldããå«ãŸããŠããããããããã¯åãã§ããããªã³ã¯ã¯ç°ãªããŸãã
次ã«ããã®ã³ãŒããããã°ã©ã ã®äžéšã«è¿œå ããŸãã
helloWorld2 = "hello world"; Console.WriteLine("{0}, {1}: {2}, {3}", helloWorld, helloWorld2, helloWorld == helloWorld2, object.ReferenceEquals(helloWorld, helloWorld2));
ãããå®è¡ããŸãã ä»åãã³ãŒãã¯ã³ã³ãœãŒã«ã«æ¬¡ã®è¡ã衚瀺ããŸãã
ããã«ã¡ã¯äžçãããã«ã¡ã¯äžçïŒçå®ãçå®
åŸ ã£ãŠã HelloWorldãšHelloWorld2ãåãè¡ãåç §ããŠããããšãããããŸãããïŒ ããããããã®æ¯ãèããå¥åŠã§ããããå°ãªããšãå°ãäºæ³å€ã§ãããšæãããããããŸããã helloWorld2ã®å€ã¯ãŸã£ããå€æŽããŸããã§ããã å€ãã®äººã¯ããã®ãããªããšãèããŠããŸããŸãããå€æ°ã¯ããã§ã«hello worldãšåçã§ããã Hello Worldã«ããäžåºŠã€ã³ã¹ããŒã«ããŠãäœãå€ãããªãã¯ãã§ãã ãããç解ããŸãããã
String.Internãšã¯äœã§ããïŒ ïŒå容ããŒã«ã«é£ã³èŸŒã...ïŒ
CïŒã§æååã䜿çšããå ŽåãCLRã¯ããªãããŒãªããšãè¡ããŸããããã¯ãæååã€ã³ã¿ãŒã³ãšåŒã°ãããã®ã§ãã ããã¯ãä»»æã®æååã®ã³ããŒã1ã€ä¿åããæ¹æ³ã§ãã 100åãããã«æªãå Žåã¯100äžåã®æååå€æ°ã«æ ŒçŽããå Žåãåãå€ã䜿çšãããšãæååå€ãæ ŒçŽããããã®ã¡ã¢ãªãäœåºŠãå²ãåœãŠãããŸãã æååã€ã³ã¿ãŒã³ã¯ããã®åé¡ãåé¿ããæ¹æ³ã§ãã CLRã¯ãå容ããŒã«ãšåŒã°ããããŒãã«ããµããŒãããŸãã ãã®ããŒãã«ã«ã¯ãããã°ã©ã ã®å®è¡äžã«ããã°ã©ã ã§å®£èšãŸãã¯äœæãããåè¡ãžã®äžæã®ãªã³ã¯ã1ã€å«ãŸããŠããŸãã .NET Frameworkã¯ãã€ã³ã¿ãŒã³ããŒã«ãšå¯Ÿè©±ããããã®2ã€ã®äŸ¿å©ãªã¡ãœãããæäŸããŸãïŒ String.InternïŒïŒããã³String.IsInternedïŒïŒ ã
String.InternïŒïŒã¡ãœããã¯éåžžã«ç°¡åãªæ¹æ³ã§æ©èœããŸãã åŒæ°ãšããŠæååãæž¡ããŸãã ãã®è¡ãæ¢ã«æçããŒã«ã«ããå Žåãã¡ãœããã¯ãã®è¡ãžã®ãªã³ã¯ãè¿ããŸãã ãŸã ååšããªãå Žåã¯ãããŒã«ã«è¡ãè¿œå ãããã®ãªã³ã¯ãè¿ããŸãã 以äžã«äŸã瀺ããŸãã
Console.WriteLine(object.ReferenceEquals( String.Intern(helloWorld), String.Intern(helloWorld2)));
HelloWorldãšHelloWorld2ã2ã€ã®ç°ãªãæååãªããžã§ã¯ããåç §ããŠããå Žåã§ãããã®ã³ãŒãã¯Trueã衚瀺ããŸãããããã¯äž¡æ¹ãšãæååãHello Worldããå«ãããã§ãã
ãã°ããåæ¢ããŸãã String.InternïŒïŒã䜿çšããŠå°ãç解ãã䟡å€ããããŸããããã¯ããã®ã¡ãœãããäžèŠãããã«éè«ççãªçµæãããããå Žåãããããã§ãã ãã®åäœã®äŸã次ã«ç€ºããŸãã
string a = new string(new char[] {'a', 'b', 'c'}); object o = String.Copy(a); Console.WriteLine(object.ReferenceEquals(o, a)); String.Intern(o.ToString()); Console.WriteLine(object.ReferenceEquals(o, String.Intern(a)));
ã³ãŒããå®è¡ãããšãã³ã³ãœãŒã«ã«2è¡ãåºåãããŸãã æåã®WriteLineïŒïŒã¡ãœããã¯Falseã衚瀺ããŸãããããã¯String.CopyïŒïŒã¡ãœãããæååã®æ°ããã³ããŒãäœæããæ°ãããªããžã§ã¯ããžã®ãªã³ã¯ãè¿ãããç解ã§ããŸãã ããããæåã«String.InternïŒabout.ToStringïŒïŒïŒãå®è¡ããŠããString.InternïŒaïŒãaboutãžã®ãªã³ã¯ãè¿ãã®ã¯ãªãã§ããïŒ ããã«ã€ããŠèããããã«å°ãç«ã¡æ¢ãŸã£ãŠãã ããã ããã«3è¡è¿œå ãããšãããã¯ããã«çŽæã«åããŸãã
object o2 = String.Copy(a); String.Intern(o2.ToString()); Console.WriteLine(object.ReferenceEquals(o2, String.Intern(a)));
ãããã®ã³ãŒãè¡ã¯ãæ°ãão2ãªããžã§ã¯ãå€æ°ã§ã®ã¿åãããã«èŠããŸãã ããããæåŸã«ã WriteLineïŒïŒã¯FalseãåºåããŸãã ããã§äœãèµ·ãã£ãŠããã®ã§ããããïŒ
ãã®å°ããªæ··ä¹±ã¯ã String.InternïŒïŒãšå容ããŒã«ã®å éšã§äœãèµ·ãã£ãŠããããææ¡ããã®ã«åœ¹ç«ã¡ãŸãã èªåã§ç解ããæåã®ããšã¯ã ToStringïŒïŒã®æååãªããžã§ã¯ãã¡ãœãããåžžã«èªèº«ãžã®åç §ãè¿ãããšã§ãã å€æ°oã¯ãå€ "abc"ãå«ãæååãªããžã§ã¯ããæããŠãããããç¬èªã®ToStringïŒïŒã¡ãœãããåŒã³åºããšããã®æååãžã®ãªã³ã¯ãè¿ãããŸãã ã ããããã§äœãèµ·ãããã§ãã
æåã¯ããabcããå«ãè¡çªå·1ã®ãªããžã§ã¯ããæããŸãã å€æ°oã¯ããabcããå«ãè¡çªå·2ã®å¥ã®ãªããžã§ã¯ããæããŸãã String.InternïŒo.ToStringïŒïŒïŒãåŒã³åºããšãè¡çªå·2ãžã®åç §ãã€ã³ã¿ãŒã³ããŒã«ã«è¿œå ãããŸãã ããã§ãè¡çªå·2ã®ãªããžã§ã¯ããå容ããŒã«ã«ããå Žåããã€ã§ããã©ã¡ãŒã¿ãŒãabcãã§åŒã³åºããString.InternïŒïŒã¯è¡çªå·2ã®ãªããžã§ã¯ããžã®åç §ãè¿ããŸãã
ãããã£ãŠãå€æ°oãšString.InternïŒaïŒãReferenceEqualsïŒïŒã¡ãœããã«æž¡ããšã String.InternïŒaïŒã¯è¡çªå·2ã®ãªããžã§ã¯ããžã®åç §ãè¿ããããã Trueãè¿ããŸãã ããã§ãæ°ããå€æ°o2ãäœæãã String.CopyïŒïŒã¡ãœããã䜿çšããŠå¥ã®Stringãªããžã§ã¯ããäœæããŸããã ããã¯ãè¡çªå·3ã®ãªããžã§ã¯ãã«ãªããæååãabcããå«ãŸããŸãã String.InternïŒo2.ToStringïŒïŒïŒã®åŒã³åºãã¯ããabcãããã§ã«ååšãããããä»åã¯å容ããŒã«ã«äœãè¿œå ããŸããããè¡çªå·2ãžã®ãã€ã³ã¿ãŒãè¿ããŸãã
ãããã£ãŠããã®InternïŒïŒã®åŒã³åºãã¯å®éã«ã¯è¡çªå·2ãžã®åç §ãè¿ããŸãããå€æ°ã«å²ãåœãŠã代ããã«ç Žæ£ããŸãã 次ã®ãããªããšãã§ããŸãïŒ string q = String.InternïŒo2.ToStringïŒïŒïŒ ãããã«ããã qå€æ°ãè¡ïŒ2ã®ãªããžã§ã¯ããžã®åç §ã«ãªããŸãã ããããæåŸã®WriteLineïŒïŒã Falseãåºåããçç±ã§ããããã¯ã3è¡ç®ã®ãªã³ã¯ãš2è¡ç®ã®ãªã³ã¯ã®æ¯èŒã§ããããã§ãã
String.IsInternedïŒïŒã䜿çšããŠãæååãã€ã³ã¿ãŒã³ããŒã«ã«ãããã©ããã確èªããŸã
ããå°ãé説çãªååã®ã¡ãœããããããŸããããã¯ãã€ã³ã¿ãŒã³ãããæååãæäœãããšãã«äŸ¿å©ã§ãïŒ String.IsInternedïŒïŒ ã æååãªããžã§ã¯ããžã®åç §ãåããŸãã ãã®æååãå容ããŒã«ã«ããå Žåãå容ãããæååã®æååãžã®åç §ãè¿ããŸã;å容ããŒã«ã«ãŸã ãªãå Žåãã¡ãœããã¯nullãè¿ããŸã ã
ååãå°ãéè«ççã«èãããçç±ã¯ããã®ã¡ãœããã¯ãIsãã§å§ãŸãããå€ãã®ããã°ã©ããæåŸ ããããã«ããŒã«åãè¿ããªãããã§ãã
IsInternedïŒïŒã¡ãœããã䜿çšããå ŽåãnullåäœæŒç®å- ??ã䜿çšããŠãæååãå容ããŒã«ã«ãªãããšã衚瀺ãããšäŸ¿å©ã§ãã ã ããšãã°ã次ã®ããã«æžãïŒ
string o = String.IsInterned(str) ?? "not interned";
çŸåšã IsInternedïŒïŒã®çµæã¯ããŒãã§ãªãå Žåã¯å€æ°oã«æ»ããå容ããŒã«ã«è¡ããªãå Žåã¯ã¹ããªã³ã°ãnot internedãã«æ»ããŸãã
ãããè¡ãããªãå Žåã Console.WriteLineïŒïŒã¡ãœããã¯ç©ºã®è¡ãåºåããŸãïŒ nullãæ€åºããããšãã®ã¡ãœãããè¡ããŸã ïŒã
String.IsInternedïŒïŒã®ä»çµã¿ã®ç°¡åãªäŸã次ã«ç€ºããŸãã
string s = new string(new char[] {'x', 'y', 'z'}); Console.WriteLine(String.IsInterned(s) ?? "not interned"); String.Intern(s); Console.WriteLine(String.IsInterned(s) ?? "not interned"); Console.WriteLine(object.ReferenceEquals( String.IsInterned(new string(new char[] { 'x', 'y', 'z' })), s));
æåã®WriteLineïŒïŒã¹ããŒãã¡ã³ãã¯ããxyzããã€ã³ã¿ãŒã³ ã¡ã³ãããŒã«ã«ãŸã ãªããããã³ã³ãœãŒã«ã«ãnot interned ããšè¡šç€ºãããŸãã 2çªç®ã®WriteLineïŒïŒã¹ããŒãã¡ã³ãã¯ã ã€ã³ã¿ãŒã³ã¡ã³ãããŒã«ã«æ¢ã«ãxyzããå«ãŸããŠããããããxyzããåºåããŸãã ãŸãã3çªç®ã®WriteLineïŒïŒã¯ããªããžã§ã¯ãsãå容ããŒã«ã«è¿œå ããããªããžã§ã¯ããæãããã TrueãåºåããŸãã
ãªãã©ã«ã€ã³ã¿ãŒã³ãèªåçã«
ã¡ãœããã®æåŸã«1è¡ã ãè¿œå ããŠãããã°ã©ã ãå床å®è¡ããããšã«ããïŒ
onsole.WriteLine(object.ReferenceEquals("xyz", ));
å®å šã«äºæããªãããšãèµ·ãããŸãïŒ
ããã°ã©ã ã¯ãã€ã³ã¿ãŒã³ããªãããšè¡šç€ºããããšã¯ãªããæåŸã®2ã€ã®WriteLineïŒïŒã¡ãœããã¯Falseã衚瀺ããŸãïŒ æåŸã®è¡ãã³ã¡ã³ãã¢ãŠããããšãããã°ã©ã ã¯æåŸ ã©ããã«åäœããŸãã ãªãïŒïŒ ããã°ã©ã ã®æåŸã«ã³ãŒããè¿œå ãããšããã®äžã®ã³ãŒãããã°ã©ã ã®åäœãã©ã®ããã«å€ãããŸãããïŒ ããã¯éåžžã«å¥åŠã§ãïŒ
ããã«åããŠåºäŒã£ããšãã¯æ¬åœã«å¥åŠã«æããŸãããæ¬åœã«çã«ããªã£ãŠããŸãã ããã°ã©ã å šäœã®åäœãå€æŽããçç±ã¯ãã³ãŒãã«ãªãã©ã«ãxyzããå«ãŸããŠããããã§ãã ãããŠãããã°ã©ã ã«ãªãã©ã«ãè¿œå ãããšãCLRã¯ããã°ã©ã ãéå§ããåã§ãã£ãŠãããããèªåçã«ã€ã³ã¿ãŒã³ããŒã«ã«è¿œå ããŸãã ãã®è¡ã«ã³ã¡ã³ãããŠãããã°ã©ã ãããªãã©ã«ãåé€ãããšãå容ããŒã«ã«ã¯æååãxyzããå«ãŸããªããªããŸãã
ãxyzãã¯ããã°ã©ã ã®èµ·åæã«å容ããŒã«ã«æ¢ã«ããããšãç解ããŸãããã®æååã¯ã³ãŒãå ã§ãªãã©ã«ãšããŠè¡šç€ºããããããããã°ã©ã ã®åäœã®ãã®ãããªå€æŽã¯ããã«æããã«ãªããŸãã String.IsInternedïŒsïŒã¯nullãè¿ããªããªããŸããã 代ããã«ããªãã©ã«ãxyzããžã®åç §ãè¿ããŸããããã¯ã ReferenceEqualsïŒïŒãFalseãè¿ãçç±ã説æããŸãã ããã¯ãæååsãå容ããŒã«ã«è¿œå ãããªãããã§ãïŒãxyzãã¯ãã§ã«ããŒã«å ã«ãããå¥ã®ãªããžã§ã¯ããæããŠããïŒã
ã³ã³ãã€ã©ã¯ããªããæã£ãŠãããããè³¢ãã§ãïŒ
ã³ãŒãã®æçµè¡ã次ã®ããã«å€æŽããŸãã
Console.WriteLine( object.ReferenceEquals("x" + "y" + "z", s));
ããã°ã©ã ãå®è¡ããŸãã ãªãã©ã«ãxyzãã䜿çšããŠãããã®ããã«æ©èœããŸãã +ã¯æŒç®åã§ã¯ãããŸãããïŒ ããã¯ãå®è¡æã«CLRã§å®è¡ãããã¡ãœããã§ã¯ãããŸãããïŒ ãã®å Žåããªãã©ã«ãxyzãã®å åšåãé²ãã³ãŒããå¿ èŠã§ãã
å®éãããã¯"x" + "y" + "z"ãString.FormatïŒ "{0} {1} {2}"ã 'x'ã 'y'ã 'z'ïŒã«çœ®ãæãããšèµ·ãããŸãã ã³ãŒãã®äž¡æ¹ã®è¡ã¯ãxyzããè¿ããŸãã ãªãã +æŒç®åãé£çµããŠãæåå "xyz"ã䜿çšããŠããããã«æ¯ãèãã®ããå®è¡æã«String.FormatïŒïŒãå®è¡ãããã®ã¯ãªãã§ããïŒ
ãã®è³ªåã«çããæãç°¡åãªæ¹æ³ã¯ãã³ãŒãâ xâ +â yâ +â zâãã³ã³ãã€ã«ãããšãã«å®éã«åŸããããã®ã確èªããããšã§ãã
Program.csïŒ
using System; class Program { public static void Main() { Console.WriteLine("x" + "y" + "z"); } }
次ã®ã¹ãããã¯ãã³ã³ãã€ã©ãŒãå®è¡å¯èœãªã¿ã€ãã®ã¢ããªã±ãŒã·ã§ã³ãã³ã³ãã€ã«ããããšã確èªããããšã§ãã ãã®ããã«ãMSILéã¢ã»ã³ãã©ãŒã§ããILDasm.exeã䜿çšããŸãã ãã®ããŒã«ã¯ãVisual Studioã®ãã¹ãŠã®ããŒãžã§ã³ïŒExpressãšãã£ã·ã§ã³ãå«ãïŒãšå ±ã«ã€ã³ã¹ããŒã«ãããŸãã ãŸããILã®èªã¿æ¹ãããããªããŠããäœãèµ·ãã£ãŠããã®ããç解ã§ããŸãã
Ildasm.exeãå®è¡ããŸãã 64ãããããŒãžã§ã³ã®Windowsã䜿çšããŠããå Žåã次ã®ã³ãã³ããå®è¡ããŸãã "ïŒ ProgramFilesïŒx86ïŒïŒ \ Microsoft SDKs \ Windows \ v7.0A \ Bin \ Ildasm.exe" ïŒåŒçšç¬Šãå«ãïŒã [ã¹ã¿ãŒã] >> [å®è¡]ãŠã£ã³ããŠãã ããŸãã¯ã³ãã³ãã©ã€ã³ããã 32ãããããŒãžã§ã³ã®Windowsã䜿çšããŠããå Žåã¯ã ãïŒ ProgramFilesïŒ \ Microsoft SDKs \ Windows \ v7.0A \ Bin \ ildasm.exeãã³ãã³ããå®è¡ããå¿ èŠããããŸãã
.NET Framework 3.5以åã䜿çšããŠããå Žå
.NET Framework 3.5以åã䜿çšããŠããå Žåãé£æ¥ãããã©ã«ããŒã§ildasm.exeãæ¢ãå¿
èŠãããå ŽåããããŸãã ExplorerãŠã£ã³ããŠãèµ·åããProgram Filesãã©ã«ããŒã«ç§»åããŸãã ååãšããŠãç®çã®ããã°ã©ã ã¯ãMicrosoft SDKs \ Windows \ vX.X \ binããã©ã«ããŒã«ãããŸãã ããã«ã[ã¹ã¿ãŒã]ã¡ãã¥ãŒã«ãã[Visual Studioã³ãã³ãããã³ãã]ããã³ãã³ãã©ã€ã³ãå®è¡ãããILDASMããšå
¥åããŠèµ·åã§ããŸãã
ããã¯ãILDasmã®ååå®è¡æã®å€èŠ³ã§ãã
次ã«ãã³ãŒããå®è¡å¯èœãã¡ã€ã«ã«ã³ã³ãã€ã«ããŸãã ãœãªã¥ãŒã·ã§ã³ãšã¯ã¹ãããŒã©ãŒã§ãããžã§ã¯ããã¯ãªãã¯ããŸãã[ ãããžã§ã¯ããã©ã«ããŒ]ãã£ãŒã«ãã¯[ ããããã£]ãŠã£ã³ããŠã«ãããŸãã ãããããã«ã¯ãªãã¯ããŠã³ããŒããŸãã ILDasmãŠã£ã³ããŠã«ç§»åããŠãã¡ãã¥ãŒã§[ ãã¡ã€ã«] >> [éã ]ãéžæãããã©ã«ããŒãžã®ãã¹ã貌ãä»ããŸãã 次ã«ãbinãã©ã«ããŒã«ç§»åããŸãã å®è¡å¯èœãã¡ã€ã«ã¯ã bin \ DebugãŸãã¯bin \ Releaseãã©ã«ãã®ããããã«ãªããã°ãªããŸããã å®è¡å¯èœãã¡ã€ã«ãéããŸãã ILDasmã¯ã¢ã»ã³ããªã®å 容ã衚瀺ããå¿ èŠããããŸãã
ïŒã¢ã»ã³ããªã®äœææ¹æ³ããã©ãã·ã¥ã¢ããããå¿ èŠãããå Žåã¯ã CïŒããã³.NETã¢ã»ã³ããªãšåå空éã®ç解ã«ã€ããŠãã®æçš¿ãåç §ããŠãã ããïŒã
Programã¯ã©ã¹ãå±éãã MainïŒïŒã¡ãœãããããã«ã¯ãªãã¯ããŸãã ãããã®æé ã®åŸãéã¢ã»ã³ãã«ãããã¡ãœããã³ãŒãã衚瀺ãããŸãã
ã³ãŒãå ã®ãªãã©ã«ãxyzãã®ååšã確èªããããã«ILãç¥ãå¿ èŠã¯ãããŸããã ILDasmãéããŠããããxã+ãyã+ãzãã®ä»£ããã«ãxyzãã䜿çšããããã«ã³ãŒããå€æŽãããšãILã³ãŒãã¯å®å šã«åãããã«è§£æãããŸãã ããã¯ãã³ã³ãã€ã©ãã³ã³ãã€ã«æã«ãxã+ãyã+ãzãããxyzãã«çœ®ãæããã®ã«ååãªããããxyzããåžžã«è¿ãã¡ãœããåŒã³åºãã«äœåãªæäœãè²»ããå¿ èŠããªãããã§ãã ãããŠããªãã©ã«ãããã°ã©ã ã§ã³ã³ãã€ã«ããããšãCLRã¯ããã°ã©ã ã®èµ·åæã«ãããå容ããŒã«ã«è¿œå ããŸãã
ãã®èšäºã®è³æã¯ãCïŒããã³.NETã§ã®ã¹ããªã³ã°ã€ã³ã¿ãŒã³ã«ã€ããŠã®è¯ãã¢ã€ãã¢ãæäŸãããã®ã§ãã ååãšããŠãããã¯ã¹ããªã³ã°ã€ã³ã¿ãŒã³ã®æäœãç解ããããã«å¿ èŠä»¥äžã§ãã ããã«è©³ããç¥ãããå Žåã¯ãMSDNã®String.InternããŒãžã®ããã©ãŒãã³ã¹ã«é¢ããèæ ®äºé ã®ã»ã¯ã·ã§ã³ãåç §ããŠãã ãã ã
PSïŒç¿»èš³ã«å¯Ÿããå³ããæ ¡æ£ãšå®¢èŠ³çãªæ¹å€ã«æè¬ããŸãã