Language
Note
ãã®ããŒãžã«ã¢ã¯ã»ã¹ããã«ã¯ãæ¿èªãå¿ èŠã§ãã ãµã€ã³ã€ã³ãŸãã¯ãã£ã¬ã¯ããªã®å€æŽã詊ãããšãã§ããŸãã
ãã®ããŒãžã«ã¢ã¯ã»ã¹ããã«ã¯ãæ¿èªãå¿ èŠã§ãã ãã£ã¬ã¯ããªã®å€æŽã詊ãããšãã§ããŸãã
.NET ã¯èšèªã«äŸåããŸããã ã€ãŸããéçºè ã¯ãC#ãF#ãVisual Basic ãªã©ã® .NET å®è£ ã察象ãšããå€ãã®èšèªã®ããããã§éçºã§ããŸãã .NET å®è£ çšã«éçºãããã¯ã©ã¹ ã©ã€ãã©ãªã®åãšã¡ã³ããŒã«ã¢ã¯ã»ã¹ã§ããŸããæåã«èšè¿°ãããèšèªãç¥ããªããŠããå ã®èšèªã®èŠåã«åŸãå¿ èŠã¯ãããŸããã ã³ã³ããŒãã³ãéçºè ã®å Žåã¯ããã®èšèªã«é¢ä¿ãªããä»»æã® .NET ã¢ããªããã³ã³ããŒãã³ãã«ã¢ã¯ã»ã¹ã§ããŸãã
泚
ãã®èšäºã®æåã®éšåã§ã¯ãèšèªã«äŸåããªãã³ã³ããŒãã³ããã€ãŸããä»»æã®èšèªã§èšè¿°ãããã¢ããªã§äœ¿çšã§ããã³ã³ããŒãã³ãã®äœæã«ã€ããŠèª¬æããŸãã è€æ°ã®èšèªã§èšè¿°ããããœãŒã¹ ã³ãŒããã 1 ã€ã®ã³ã³ããŒãã³ããŸãã¯ã¢ããªãäœæããããšãã§ããŸãããã®èšäºã®ç¬¬ 2 éšã®ã èšèªéçžäºéçšæ§ ããåç §ããŠãã ããã
ä»»æã®èšèªã§èšè¿°ãããä»ã®ãªããžã§ã¯ããšå®å šã«å¯Ÿè©±ããã«ã¯ããªããžã§ã¯ããåŒã³åºãå ã«ãã¹ãŠã®èšèªã«å ±éããæ©èœã®ã¿ãåŒã³åºãå ã«å ¬éããå¿ èŠããããŸãã ãã®äžè¬çãªæ©èœã»ããã¯ãå ±é èšèªä»æ§ (CLS) ã«ãã£ãŠå®çŸ©ãããŸããããã¯ãçæãããã¢ã»ã³ããªã«é©çšãããäžé£ã®èŠåã§ãã å ±éèšèªä»æ§ã¯ã ECMA-335 Standard: Common Language Infrastructure ã®ããŒãã£ã·ã§ã³ Iã7 ãã 11 ã®å¥ã§å®çŸ©ãããŠããŸãã
ã³ã³ããŒãã³ããå ±éèšèªä»æ§ã«æºæ ããŠããå Žåã¯ãCLS ã«æºæ ããããšãä¿èšŒãããCLS ããµããŒãããä»»æã®ããã°ã©ãã³ã°èšèªã§èšè¿°ãããã¢ã»ã³ããªå ã®ã³ãŒãããã¢ã¯ã»ã¹ã§ããŸãã CLSCompliantAttribute 屿§ããœãŒã¹ ã³ãŒãã«é©çšããããšã§ãã³ã³ãã€ã«æã«ã³ã³ããŒãã³ããå ±éèšèªä»æ§ã«æºæ ããŠãããã©ããã倿ã§ããŸãã 詳现ã«ã€ããŠã¯ãã CLSCompliantAttribute 屿§ããåç §ããŠãã ããã
CLS ã³ã³ãã©ã€ã¢ã³ã¹èŠå
ãã®ã»ã¯ã·ã§ã³ã§ã¯ãCLS æºæ ã³ã³ããŒãã³ããäœæããããã®èŠåã«ã€ããŠèª¬æããŸãã èŠåã®å®å šãªäžèЧã«ã€ããŠã¯ã ECMA-335 Standard: Common Language Infrastructure ã®ããŒãã£ã·ã§ã³ I 第 11 é ãåç §ããŠãã ããã
泚
å ±éèšèªä»æ§ã§ã¯ãã³ã³ã·ã¥ãŒã㌠(CLS æºæ ã®ã³ã³ããŒãã³ãã«ããã°ã©ã ã§ã¢ã¯ã»ã¹ããéçºè )ããã¬ãŒã ã¯ãŒã¯ (èšèªã³ã³ãã€ã©ã䜿çšã㊠CLS æºæ ã©ã€ãã©ãªãäœæããŠããéçºè )ããšã¯ã¹ãã³ã㌠(èšèªã³ã³ãã€ã©ã CLS æºæ ã³ã³ããŒãã³ããäœæããã³ãŒã ããŒãµãŒãªã©ã®ããŒã«ãäœæããŠããéçºè ) ã«é©çšããã CLS æºæ ã®åèŠåã«ã€ããŠèª¬æããŸãã ãã®èšäºã§ã¯ããã¬ãŒã ã¯ãŒã¯ã«é©çšãããèŠåã«çŠç¹ãåœãŠãŸãã ãã ãããšã¯ã¹ãã³ããŒã«é©çšãããèŠåã®äžéšã¯ ãReflection.Emit ã䜿çšããŠäœæãããã¢ã»ã³ããªã«ãé©çšãããå Žåãããããšã«æ³šæããŠãã ããã
èšèªã«äŸåããªãã³ã³ããŒãã³ããèšèšããã«ã¯ãCLS æºæ ã®èŠåãã³ã³ããŒãã³ãã®ãããªã㯠ã€ã³ã¿ãŒãã§ã€ã¹ã«ã®ã¿é©çšããå¿ èŠããããŸãã ãã©ã€ããŒãå®è£ ã¯ã仿§ã«æºæ ããå¿ èŠã¯ãããŸããã
Von Bedeutung
CLS ã³ã³ãã©ã€ã¢ã³ã¹ã®èŠåã¯ãã³ã³ããŒãã³ãã®ãããªã㯠ã€ã³ã¿ãŒãã§ã€ã¹ã«ã®ã¿é©çšããããã©ã€ããŒãå®è£ ã«ã¯é©çšãããŸããã
ããšãã°ã Byte 以å€ã®ç¬Šå·ãªãæŽæ°ã¯ CLS ã«æºæ ããŠããŸããã æ¬¡ã®äŸã®Person ã¯ã©ã¹ã¯Ageåã®UInt16 ããããã£ãå
¬éããŠãããããæ¬¡ã®ã³ãŒãã¯ã³ã³ãã€ã©èŠåã衚瀺ããŸãã
using System;
[assembly: CLSCompliant(true)]
public class Person
{
private UInt16 personAge = 0;
public UInt16 Age
{ get { return personAge; } }
}
// The attempt to compile the example displays the following compiler warning:
// Public1.cs(10,18): warning CS3003: Type of 'Person.Age' is not CLS-compliant
<Assembly: CLSCompliant(True)>
Public Class Person
Private personAge As UInt16
Public ReadOnly Property Age As UInt16
Get
Return personAge
End Get
End Property
End Class
' The attempt to compile the example displays the following compiler warning:
' Public1.vb(9) : warning BC40027: Return type of function 'Age' is not CLS-compliant.
'
' Public ReadOnly Property Age As UInt16
' ~~~
Person ã¯ã©ã¹ã CLS æºæ ã«ããã«ã¯ãAge ããããã£ã®åã UInt16 ãã Int16 (CLS æºæ ã® 16 ããã笊å·ä»ãæŽæ°) ã«å€æŽããŸãã ãã©ã€ããŒã personAge ãã£ãŒã«ãã®åã倿Žããå¿
èŠã¯ãããŸããã
using System;
[assembly: CLSCompliant(true)]
public class Person
{
private UInt16 personAge = 0;
public Int16 Age => (Int16)personAge;
}
<Assembly: CLSCompliant(True)>
Public Class Person
Private personAge As UInt16
Public ReadOnly Property Age As Int16
Get
Return CType(personAge, Int16)
End Get
End Property
End Class
ã©ã€ãã©ãªã®ãããªã㯠ã€ã³ã¿ãŒãã§ã€ã¹ã¯ã次ã®èŠçŽ ã§æ§æãããŸãã
ãããªã㯠ã¯ã©ã¹ã®å®çŸ©ã
ãããªã㯠ã¯ã©ã¹ã®ãããªã㯠ã¡ã³ããŒã®å®çŸ©ãšã掟çã¯ã©ã¹ã«ã¢ã¯ã»ã¹ã§ããã¡ã³ããŒã®å®çŸ© (ã€ãŸããä¿è·ãããã¡ã³ããŒ)ã
ãããªã㯠ã¯ã©ã¹ã®ãããªã㯠ã¡ãœããã®ãã©ã¡ãŒã¿ãŒãšæ»ãå€ã®åãããã³æŽŸçã¯ã©ã¹ããã¢ã¯ã»ã¹ã§ããã¡ãœããã®ãã©ã¡ãŒã¿ãŒãšæ»ãå€ã®åã
CLS æºæ ã®èŠåãæ¬¡ã®è¡šã«ç€ºããŸãã ã«ãŒã«ã®ããã¹ãã¯ã Ecma-335 Standard: Common Language Infrastructure (Ecma International ã® Copyright 2012) ããéèªçã«åãããŸãã ãããã®èŠåã®è©³çްã«ã€ããŠã¯ã次ã®ã»ã¯ã·ã§ã³ãåç §ããŠãã ããã
| ã«ããŽãª | åç §å | ã«ãŒã« | ã«ãŒã«çªå· |
|---|---|---|---|
| ã¢ã¯ã»ã·ããªã㣠| ã¡ã³ããŒã®ã¢ã¯ã»ã·ããªã㣠| ç¶æ¿ãããã¡ãœããããªãŒããŒã©ã€ãããå Žåãã¢ã¯ã»ã·ããªãã£ã¯å€æŽããŠã¯ãããªãããã ããå¥ã®ã¢ã»ã³ããªããç¶æ¿ãããã¡ãœãããã¢ã¯ã»ã·ããªã㣠family-or-assembly ã§ãªãŒããŒã©ã€ãããå Žåã¯é€ãã ãã®å ŽåããªãŒããŒã©ã€ãã«ã¯ã¢ã¯ã»ã·ããªã㣠familyããããŸãã |
10 |
| ã¢ã¯ã»ã·ããªã㣠| ã¡ã³ããŒã®ã¢ã¯ã»ã·ããªã㣠| åãšã¡ã³ããŒã®å¯èŠæ§ãšã¢ã¯ã»ã·ããªãã£ã¯ãã¡ã³ããŒèªäœãå¯èŠã§ã¢ã¯ã»ã¹å¯èœã§ãããšãã«ãã¡ã³ããŒã®ã·ã°ããã£ã«ããåãå¯èŠã§ã¢ã¯ã»ã¹å¯èœã§ããããã«ãã¹ãã§ãã ããšãã°ãã¢ã»ã³ããªã®å€éšã§åç §ã§ãããããªã㯠ã¡ãœããã«ã¯ããã®åãã¢ã»ã³ããªå ã§ã®ã¿è¡šç€ºãããåŒæ°ã¯å«ãŸããŸããã ä»»æã®ã¡ã³ããŒã®ã·ã°ããã£ã§äœ¿çšãããã€ã³ã¹ã¿ã³ã¹åããããžã§ããªãã¯åãæ§æããåã®å¯èŠæ§ãšã¢ã¯ã»ã·ããªãã£ã¯ãã¡ã³ããŒèªäœãå¯èŠã§ã¢ã¯ã»ã¹å¯èœãªéã«ã¯ãåžžã«å¯èŠã§ã¢ã¯ã»ã¹å¯èœã§ãªããã°ãªããŸããã ããšãã°ãã¢ã»ã³ããªã®å€éšã«è¡šç€ºãããã¡ã³ããŒã®ã·ã°ããã£ã«ååšããã€ã³ã¹ã¿ã³ã¹åããããžã§ããªãã¯åã«ã¯ãã¢ã»ã³ããªå ã§ã®ã¿åç §å¯èœãªåã®ãžã§ããªãã¯åŒæ°ã¯å«ãŸããŸããã | 12 |
| é å | é å | é åã«ã¯ CLS æºæ åã®èŠçŽ ããããé åã®ãã¹ãŠã®æ¬¡å ã®äžé㯠0 ã§ããå¿ èŠããããŸãã ãªãŒããŒããŒããåºå¥ããããã«å¿ èŠãªã®ã¯ãé ç®ãé åã§ãããé åã®èŠçŽ åã§ãããšããäºå®ã ãã§ãã ãªãŒããŒããŒãã 2 ã€ä»¥äžã®é ååã«åºã¥ããŠããå ŽåãèŠçŽ åã¯ååä»ãåã§ããå¿ èŠããããŸãã | 16 |
| 屿§ | 屿§ | 屿§ã¯ãSystem.AttributeåããŸãã¯ãã®åããç¶æ¿ãããåã§ããå¿ èŠããããŸãã | 41 |
| 屿§ | 屿§ | CLS ã§ã¯ãã«ã¹ã¿ã 屿§ã®ãšã³ã³ãŒãã®ãµãã»ããã®ã¿ãèš±å¯ãããŸãã ãããã®ãšã³ã³ãŒãã«è¡šç€ºãããå¯äžã®å㯠(ããŒãã£ã·ã§ã³ IV ãåç §) ã§ãã System.Typeã System.Stringã System.Charã System.Booleanã System.Byteã System.Int16ã System.Int32ã System.Int64ã System.Singleã System.Doubleãããã³ CLS æºæ ã®åºæ¬æŽæ°åã«åºã¥ãåæåã | 34 |
| 屿§ | 屿§ | CLS ã§ã¯ããããªãã¯ã«è¡šç€ºãããå¿
èŠãªä¿®é£Ÿå (modreqãããŒãã£ã·ã§ã³ II ãåç
§) ã¯èš±å¯ãããŸããããèªèã§ããªãçç¥å¯èœãªä¿®é£Ÿå (modoptãããŒãã£ã·ã§ã³ II ãåç
§) ã¯èš±å¯ãããŸãã |
35 |
| ã³ã³ã¹ãã©ã¯ã¿ãŒ | ã³ã³ã¹ãã©ã¯ã¿ãŒ | ãªããžã§ã¯ã ã³ã³ã¹ãã©ã¯ã¿ãŒã¯ãç¶æ¿ãããã€ã³ã¹ã¿ã³ã¹ ããŒã¿ãžã®ã¢ã¯ã»ã¹ãçºçããåã«ããã®åºåºã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºãå¿ èŠããããŸãã (ããã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒãæã€å¿ èŠã®ãªãå€åã«ã¯é©çšãããŸãã)ã | 21 (äºåäž) |
| ã³ã³ã¹ãã©ã¯ã¿ãŒ | ã³ã³ã¹ãã©ã¯ã¿ãŒ | ãªããžã§ã¯ã ã³ã³ã¹ãã©ã¯ã¿ãŒã¯ããªããžã§ã¯ãã®äœæã®äžéšãšããŠä»¥å€ã¯åŒã³åºãããããªããžã§ã¯ã㯠2 ååæåãããŸããã | 22 |
| åæå | åæ | åæåã®åºã«ãªãåã¯çµã¿èŸŒã¿ã® CLS æŽæ°åã§ããã£ãŒã«ãã®åå㯠"value__" ã§ããã®ãã£ãŒã«ã㯠RTSpecialNameããŒã¯ãããŸãã |
7 |
| åæå | åæ |
System.FlagsAttributeã®æç¡ (ããŒãã£ã·ã§ã³ IV ã©ã€ãã©ãªãåç
§) ã«ã¹ã¿ã 屿§ã«ãã£ãŠç€ºãããã2 çš®é¡ã®åæåããããŸãã 1 ã€ã¯ååä»ãæŽæ°å€ã衚ããŸãããã 1 ã€ã¯ãååã®ãªãå€ãçæããããã«çµã¿åãããããšãã§ããååä»ãããã ãã©ã°ã衚ããŸãã
enumã®å€ã¯ãæå®ããå€ã«éå®ãããŸããã |
8 |
| åæå | åæ | åæåã®ãªãã©ã«éçãã£ãŒã«ãã¯ãåæåèªäœã®åãæã€å¿ èŠããããŸãã | 9 |
| ã€ãã³ã | ã€ãã³ã | ã€ãã³ããå®è£
ããã¡ãœããã¯ãã¡ã¿ããŒã¿å
ã§ SpecialName ããŒã¯ãããŸãã |
äºåä¹ |
| ã€ãã³ã | ã€ãã³ã | ã€ãã³ããšãã®ã¢ã¯ã»ãµãŒã®ã¢ã¯ã»ã·ããªãã£ã¯åãã§ããå¿ èŠããããŸãã | 30 |
| ã€ãã³ã | ã€ãã³ã | ã€ãã³ãã® add ã¡ãœãããš remove ã¡ãœããã®äž¡æ¹ãååšããããååšããªããã®ã©ã¡ããã§ããå¿
èŠããããŸãã |
31 |
| ã€ãã³ã | ã€ãã³ã | ã€ãã³ãã® add ã¡ãœãããš remove ã¡ãœããã¯ããããããã€ãã³ãã®åãå®çŸ©ãã System.Delegate ããæŽŸçããåãæã€ 1 ã€ã®ãã©ã¡ãŒã¿ãŒãåãåãå¿
èŠããããŸãã |
32 |
| ã€ãã³ã | ã€ãã³ã | ã€ãã³ãã¯ãç¹å®ã®ååä»ããã¿ãŒã³ã«åŸãå¿ èŠããããŸãã CLS èŠå 29 ã§åç §ããã SpecialName 屿§ã¯ãé©åãªååã®æ¯èŒã§ã¯ç¡èŠãããèå¥åèŠåã«åŸãå¿ èŠããããŸãã | 33 |
| äŸå€ | äŸå€ | ã¹ããŒããããªããžã§ã¯ãã¯ã System.Exception åãŸãã¯ãããç¶æ¿ããåã§ããå¿ èŠããããŸãã ãã ããCLS ã«æºæ ããã¡ãœããã¯ãä»ã®çš®é¡ã®äŸå€ã®äŒéããããã¯ããå¿ èŠã¯ãããŸããã | 40 |
| å šè¬ | CLS ã³ã³ãã©ã€ã¢ã³ã¹èŠå | CLS èŠåã¯ãå®çŸ©ã¢ã»ã³ããªã®å€éšã§ã¢ã¯ã»ã¹å¯èœãŸãã¯åç §å¯èœãªåã®éšåã«ã®ã¿é©çšãããŸãã | 1 |
| å šè¬ | CLS ã³ã³ãã©ã€ã¢ã³ã¹èŠå | CLS éæºæ åã®ã¡ã³ããŒã CLS æºæ ãšæå®ããªãã | 2 |
| ãžã§ããªã㯠| ãžã§ããªãã¯åãšã¡ã³ã㌠| å ¥ãåã«ãªã£ãåã«ã¯ãå€åŽã®åãšåæ°ä»¥äžã®ãžã§ããªã㯠ãã©ã¡ãŒã¿ãŒãå«ãŸããŠããå¿ èŠããããŸãã å ¥ãåã«ãããåã®ãžã§ããªã㯠ãã©ã¡ãŒã¿ãŒã¯ããããå²ãåã®ãžã§ããªã㯠ãã©ã¡ãŒã¿ãŒãšãäœçœ®ã«ãã£ãŠå¯Ÿå¿ããŸãã | 42 |
| ãžã§ããªã㯠| ãžã§ããªãã¯åãšã¡ã³ã㌠| ãžã§ããªãã¯åã®ååã¯ãäžèšã§å®çŸ©ããèŠåã«åŸã£ãŠãå ¥ãåã«ãªã£ãŠããªãåã§å®£èšãããåãã©ã¡ãŒã¿ãŒã®æ°ããŸãã¯å ¥ãåã«ãªã£ãå Žåã«æ°ããåã«å°å ¥ãããåãã©ã¡ãŒã¿ãŒã®æ°ããšã³ã³ãŒãããå¿ èŠããããŸãã | 43 |
| ãžã§ããªã㯠| ãžã§ããªãã¯åãšã¡ã³ã㌠| ãžã§ããªãã¯åã¯ãåºæ¬åã®å¶çŽããŸãã¯ã€ã³ã¿ãŒãã§ã€ã¹ããžã§ããªãã¯åã®å¶çŽã«ãã£ãŠæºããããããšãä¿èšŒããããã«ååãªå¶çŽãå宣èšããå¿ èŠããããŸãã | 44 |
| ãžã§ããªã㯠| ãžã§ããªãã¯åãšã¡ã³ã㌠| ãžã§ããªã㯠ãã©ã¡ãŒã¿ãŒã®å¶çŽãšããŠäœ¿çšãããåã¯ãããèªäœã CLS ã«æºæ ããŠããå¿ èŠããããŸãã | 45 |
| ãžã§ããªã㯠| ãžã§ããªãã¯åãšã¡ã³ã㌠| ã€ã³ã¹ã¿ã³ã¹åããããžã§ããªãã¯åã®ã¡ã³ã㌠(å ¥ãåã«ãªã£ãåãå«ã) ã®å¯èŠæ§ãšã¢ã¯ã»ã·ããªãã£ã¯ããžã§ããªãã¯å宣èšå šäœã§ã¯ãªããç¹å®ã®ã€ã³ã¹ã¿ã³ã¹åã®ã¹ã³ãŒããšèŠãªãããŸãã ãããæ³å®ãããšãCLS èŠå 12 ã®å¯èŠæ§ãšã¢ã¯ã»ã·ããªãã£ã®èŠåãåŒãç¶ãé©çšãããŸãã | 46 |
| ãžã§ããªã㯠| ãžã§ããªãã¯åãšã¡ã³ã㌠| æœè±¡ã¡ãœãããŸãã¯ä»®æ³ãžã§ããªã㯠ã¡ãœããããšã«ãæ¢å®ã®å ·è±¡ (éabstract) å®è£ ãååšããå¿ èŠããããŸãã | 47 |
| ã€ã³ã¿ãŒãã§ã€ã¹ | ã€ã³ã¿ãŒãã§ã€ã¹ | CLS æºæ ã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ããããã«ãé CLS æºæ ã¡ãœããã®å®çŸ©ã¯å¿ èŠãããŸããã | 18 |
| ã€ã³ã¿ãŒãã§ã€ã¹ | ã€ã³ã¿ãŒãã§ã€ã¹ | CLS æºæ ã€ã³ã¿ãŒãã§ã€ã¹ã¯éçã¡ãœãããå®çŸ©ããããã£ãŒã«ããå®çŸ©ããŸããã | 19 |
| ã¡ã³ã㌠| äžè¬çãªåã¡ã³ã㌠| ã°ããŒãã«éçãã£ãŒã«ããšã¡ãœãã㯠CLS ã«æºæ ããŠããŸããã | 36 |
| ã¡ã³ã㌠| -- | ãªãã©ã«éçã®å€ã¯ããã£ãŒã«ãåæåã¡ã¿ããŒã¿ã䜿çšããŠæå®ãããŸãã CLS æºæ ãªãã©ã«ã¯ããã£ãŒã«ãåæåã¡ã¿ããŒã¿ã§ããªãã©ã«ãšãŸã£ããåãå (ãŸãã¯ããã®ãªãã©ã«ã enumã®å Žåã¯åºã«ãªãå) ã®å€ãæå®ããå¿
èŠããããŸãã |
13 |
| ã¡ã³ã㌠| äžè¬çãªåã¡ã³ã㌠| vararg å¶çŽã¯ CLS ã®äžéšã§ã¯ãªããCLS ã§ãµããŒããããŠããå¯äžã®åŒã³åºãèŠåã¯ãæšæºã®ãããŒãžãåŒã³åºãèŠåã§ãã | 15 |
| ååä»ãèŠå | ååä»ãèŠå | ã¢ã»ã³ããªã¯ãUnicode æ£èŠåãã©ãŒã ã§ãªã³ã©ã€ã³ã§äœ¿çšã§ããèå¥åã«éå§ããã³å«ãŸããããšãèš±å¯ãããŠããæåã®ã»ããã管çãã Unicode Standard3.0 ã®ãã¯ãã«ã« ã¬ããŒã 15 ã®ä»å±æž 7 ã«åŸãå¿ èŠããããŸãã èå¥åã¯ãUnicode æ£èŠååœ¢åŒ C ã§å®çŸ©ãããæ£èŠåœ¢åŒã§ããå¿ èŠããããŸããCLS ã®ç®çäžãå°æåã®ãããã³ã° (Unicode ãã±ãŒã«ãåºå¥ããªã 1 察 1 ã®å°æåãããã³ã°ã§æå®) ãåãå Žåã2 ã€ã®èå¥åã¯åãã§ãã ã€ãŸãã2 ã€ã®èå¥åã CLS ã®äžã§ç°ãªããšèŠãªãããããã«ã¯ãåã«ç°ãªãå Žåãããç°ãªãå¿ èŠããããŸãã ãã ããç¶æ¿ãããå®çŸ©ããªãŒããŒã©ã€ãããã«ã¯ãCLI ã§å ã®å®£èšã®æ£ç¢ºãªãšã³ã³ãŒãã䜿çšããå¿ èŠããããŸãã | 4 |
| ãªãŒããŒããŒã | ååä»ãèŠå | CLS æºæ ã®ã¹ã³ãŒãã«å°å ¥ããããã¹ãŠã®ååã¯ãå®å šã«ç¬ç«ããçš®é¡ã§ããå¿ èŠããããŸãããã ããååãåãã§ãªãŒããŒããŒãã«ãã£ãŠè§£æ±ºã§ããå Žåãé€ããŸãã ã€ãŸããCTS ã§ã¯ã1 ã€ã®åã§ã¡ãœãããšãã£ãŒã«ãã«åãååã䜿çšããããšãã§ããŸãããCLS ã§ã¯äœ¿çšãããŸããã | 5 |
| ãªãŒããŒããŒã | ååä»ãèŠå | CTS ã§ã¯åå¥ã®çœ²åãåºå¥ã§ããå Žåã§ãããã£ãŒã«ããšå ¥ãåã«ãªã£ãåã¯èå¥åã®æ¯èŒã ãã§åºå¥ãããŸãã åãååãæã€ã¡ãœãããããããã£ãããã³ã€ãã³ã (èå¥åã®æ¯èŒ) ã¯ãCLS èŠå 39 ã§æå®ãããŠããå Žåãé€ããæ»ãå€ã®å以äžã®éãããããŸã | 6 |
| ãªãŒããŒããŒã | ãªãŒããŒããŒã | ãªãŒããŒããŒãã§ããã®ã¯ããããã£ãšã¡ãœããã ãã§ãã | 37 |
| ãªãŒããŒããŒã | ãªãŒããŒããŒã | ããããã£ãšã¡ãœããã¯ããã©ã¡ãŒã¿ãŒã®æ°ãšåã«ã®ã¿åºã¥ããŠãªãŒããŒããŒãã§ããŸãããã ãã op_Implicit ããã³ op_Explicit ãšããååã®å€ææŒç®åã¯ãæ»ãå€ã®åã«åºã¥ããŠãªãŒããŒããŒãããããšãã§ããŸãã |
äžåå « |
| ãªãŒããŒããŒã | -- | åã§å®£èšãããŠãã 2 ã€ä»¥äžã® CLS æºæ ã¡ãœããã®ååãåãã§ãç¹å®ã®åã€ã³ã¹ã¿ã³ã¹åã®ã»ããã«å¯ŸããŠããã©ã¡ãŒã¿ãŒãšæ»ãå€ã®åãåãã§ããå Žåããããã®ã¡ãœããã¯ãã¹ãŠããããã®åã®ã€ã³ã¹ã¿ã³ã¹åã§æå³çã«åçã§ããå¿ èŠããããŸãã | 48 |
| ãããã㣠| ãããã㣠| ããããã£ã® getter ã¡ãœãããš setter ã¡ãœãããå®è£
ããã¡ãœããã¯ãã¡ã¿ããŒã¿å
ã§ SpecialName ããŒã¯ããå¿
èŠããããŸãã |
24 |
| ãããã㣠| ãããã㣠| ããããã£ã®ã¢ã¯ã»ãµãŒã¯ãã¹ãŠéçã§ãããããã¹ãŠä»®æ³ã§ãããããã¹ãŠã€ã³ã¹ã¿ã³ã¹ã§ããå¿ èŠããããŸãã | 26 |
| ãããã㣠| ãããã㣠| ããããã£ã®åã¯ãgetter ã®æ»ãå€ã®åãšãã»ãã¿ãŒã®æåŸã®åŒæ°ã®åã§ããå¿ èŠããããŸãã ããããã£ã®ãã©ã¡ãŒã¿ãŒã®åã¯ãgetter ã®ãã©ã¡ãŒã¿ãŒã®åãšãã»ãã¿ãŒã®æåŸã®ãã©ã¡ãŒã¿ãŒãé€ããã¹ãŠã®åã§ããå¿ èŠããããŸãã ãããã®åã¯ãã¹ãŠ CLS ã«æºæ ãããããŒãžã ãã€ã³ã¿ãŒã§ã¯ãªã (ã€ãŸããåç §æž¡ããããªã)ã | äºåäž |
| ãããã㣠| ãããã㣠| ããããã£ã¯ãç¹å®ã®ååä»ããã¿ãŒã³ã«åŸãå¿
èŠããããŸãã CLS èŠå 24 ã§åç
§ããã SpecialName 屿§ã¯ãé©åãªå忝èŒã§ã¯ç¡èŠãããèå¥åèŠåã«åŸãå¿
èŠããããŸãã ããããã£ã«ã¯ãgetter ã¡ãœãããã»ãã¿ãŒ ã¡ãœããããŸãã¯ãã®äž¡æ¹ãå«ãŸããŸãã |
28 |
| å倿 | å倿 | op_ImplicitãŸãã¯op_ExplicitãæäŸãããå Žåã¯ã匷å¶ãæäŸããå¥ã®ææ®µãæäŸããå¿ èŠããããŸãã | 39 |
| çš®é¡ | åããã³åã¡ã³ããŒã®ã·ã°ãã㣠| ããã¯ã¹åãããå€å㯠CLS ã«æºæ ããŠããŸããã | 3 |
| çš®é¡ | åããã³åã¡ã³ããŒã®ã·ã°ãã㣠| ã·ã°ããã£ã«è¡šç€ºããããã¹ãŠã®åã¯ãCLS ã«æºæ ããŠããå¿ èŠããããŸãã ã€ã³ã¹ã¿ã³ã¹åããããžã§ããªãã¯åãæ§æãããã¹ãŠã®åã¯ãCLS ã«æºæ ããŠããå¿ èŠããããŸãã | 11 |
| çš®é¡ | åããã³åã¡ã³ããŒã®ã·ã°ãã㣠| åæå®ãããåç §ã¯ CLS æºæ ã§ã¯ãããŸããã | 14 |
| çš®é¡ | åããã³åã¡ã³ããŒã®ã·ã°ãã㣠| 管çãããŠããªããã€ã³ã¿ãŒå㯠CLS ã«æºæ ããŠããŸããã | åäž |
| çš®é¡ | åããã³åã¡ã³ããŒã®ã·ã°ãã㣠| CLS æºæ ã®ã¯ã©ã¹ãå€ã®åãããã³ã€ã³ã¿ãŒãã§ã€ã¹ã§ã¯ãCLS ã«æºæ ããŠããªãã¡ã³ããŒã®å®è£ ã¯å¿ èŠãããŸãã | 20 |
| çš®é¡ | åããã³åã¡ã³ããŒã®ã·ã°ãã㣠| System.Object 㯠CLS ã«æºæ ããŠããŸãã ãã®ä»ã® CLS æºæ ã¯ã©ã¹ã¯ãCLS æºæ ã¯ã©ã¹ããç¶æ¿ããå¿ èŠããããŸãã | 23 |
ãµãã»ã¯ã·ã§ã³ãžã®ã€ã³ããã¯ã¹:
- åããã³åã¡ã³ããŒã®ã·ã°ããã£
- ååä»ãèŠå
- å倿
- é å
- ã€ã³ã¿ãŒãã§ã€ã¹
- åæ
- äžè¬çãªåã¡ã³ããŒ
- ã¡ã³ããŒã®ã¢ã¯ã»ã·ããªãã£
- ãžã§ããªãã¯åãšã¡ã³ããŒ
- ã³ã³ã¹ãã©ã¯ã¿ãŒ
- ããããã£
- ã€ãã³ã
- ãªãŒããŒããŒã
- äŸå€
- 屿§
åããã³åã¡ã³ããŒã®ã·ã°ããã£
System.Object å㯠CLS ã«æºæ ããŠããã.NET åã·ã¹ãã ã®ãã¹ãŠã®ãªããžã§ã¯ãåã®åºæ¬åã§ãã .NET ã§ã®ç¶æ¿ã¯ãæé»ç (ããšãã°ãString ã¯ã©ã¹ãObject ã¯ã©ã¹ããæé»çã«ç¶æ¿) ãŸãã¯æç€ºç (ããšãã°ãCultureNotFoundException ã¯ã©ã¹ã¯ãException ã¯ã©ã¹ããæç€ºçã«ç¶æ¿ããã ArgumentException ã¯ã©ã¹ããæç€ºçã«ç¶æ¿ãããŸã)ã æŽŸçåã CLS ã«æºæ ãããã«ã¯ããã®åºæ¬åã CLS ã«æºæ ããŠããå¿
èŠããããŸãã
次ã®äŸã¯ãåºæ¬åã CLS ã«æºæ ããŠããªã掟çåã瀺ããŠããŸãã ããã¯ã笊å·ãªã 32 ãããæŽæ°ãã«ãŠã³ã¿ãŒãšããŠäœ¿çšããåºæ¬ Counter ã¯ã©ã¹ãå®çŸ©ããŸãã ãã®ã¯ã©ã¹ã¯ç¬Šå·ãªãæŽæ°ãã©ããããããšã«ãã£ãŠã«ãŠã³ã¿ãŒæ©èœãæäŸãããããã¯ã©ã¹ã¯é CLS æºæ ãšããŠããŒã¯ãããŸãã ãã®çµæã掟çã¯ã©ã¹ ( NonZeroCounter) ã CLS ã«æºæ ããŠããŸããã
using System;
[assembly: CLSCompliant(true)]
[CLSCompliant(false)]
public class Counter
{
UInt32 ctr;
public Counter()
{
ctr = 0;
}
protected Counter(UInt32 ctr)
{
this.ctr = ctr;
}
public override string ToString()
{
return String.Format("{0}). ", ctr);
}
public UInt32 Value
{
get { return ctr; }
}
public void Increment()
{
ctr += (uint) 1;
}
}
public class NonZeroCounter : Counter
{
public NonZeroCounter(int startIndex) : this((uint) startIndex)
{
}
private NonZeroCounter(UInt32 startIndex) : base(startIndex)
{
}
}
// Compilation produces a compiler warning like the following:
// Type3.cs(37,14): warning CS3009: 'NonZeroCounter': base type 'Counter' is not
// CLS-compliant
// Type3.cs(7,14): (Location of symbol related to previous warning)
<Assembly: CLSCompliant(True)>
<CLSCompliant(False)> _
Public Class Counter
Dim ctr As UInt32
Public Sub New
ctr = 0
End Sub
Protected Sub New(ctr As UInt32)
ctr = ctr
End Sub
Public Overrides Function ToString() As String
Return String.Format("{0}). ", ctr)
End Function
Public ReadOnly Property Value As UInt32
Get
Return ctr
End Get
End Property
Public Sub Increment()
ctr += CType(1, UInt32)
End Sub
End Class
Public Class NonZeroCounter : Inherits Counter
Public Sub New(startIndex As Integer)
MyClass.New(CType(startIndex, UInt32))
End Sub
Private Sub New(startIndex As UInt32)
MyBase.New(CType(startIndex, UInt32))
End Sub
End Class
' Compilation produces a compiler warning like the following:
' Type3.vb(34) : warning BC40026: 'NonZeroCounter' is not CLS-compliant
' because it derives from 'Counter', which is not CLS-compliant.
'
' Public Class NonZeroCounter : Inherits Counter
' ~~~~~~~~~~~~~~
ã¡ãœããã®æ»ãå€ã®åãããããã£åãªã©ãã¡ã³ã㌠ã·ã°ããã£ã«è¡šç€ºããããã¹ãŠã®å㯠CLS ã«æºæ ããŠããå¿ èŠããããŸãã ããã«ããžã§ããªãã¯åã®å Žå:
ã€ã³ã¹ã¿ã³ã¹åããããžã§ããªãã¯åãæ§æãããã¹ãŠã®åã¯ãCLS ã«æºæ ããŠããå¿ èŠããããŸãã
ãžã§ããªã㯠ãã©ã¡ãŒã¿ãŒã®å¶çŽãšããŠäœ¿çšããããã¹ãŠã®åã¯ãCLS ã«æºæ ããŠããå¿ èŠããããŸãã
.NET å ±éåã·ã¹ãã ã«ã¯ãå ±éèšèªã©ã³ã¿ã€ã ã«ãã£ãŠçŽæ¥ãµããŒããããã¢ã»ã³ããªã®ã¡ã¿ããŒã¿ã§ç¹å¥ã«ãšã³ã³ãŒããããå€ãã®çµã¿èŸŒã¿åãå«ãŸããŠããŸãã ãããã®çµã¿èŸŒã¿åã®ãã¡ã次ã®è¡šã«ç€ºãå㯠CLS ã«æºæ ããŠããŸãã
| CLS æºæ å | 説æ |
|---|---|
| ãã€ã | 8 ããã笊å·ãªãæŽæ° |
| Int16 | 16 ããã笊å·ä»ãæŽæ° |
| Int32 | 32 ããã笊å·ä»ãæŽæ° |
| Int64 | 64 ããã笊å·ä»ãæŽæ° |
| åå | å粟床浮åå°æ°ç¹æ°å€ |
| ã·ã³ã°ã« | å粟床浮åå°æ°ç¹æ°å€ |
| ããã« | å粟床浮åå°æ°ç¹æ°å€ |
| ããŒãªã¢ã³ | true ãŸã㯠false å€å |
| æå | UTF-16 ã§ãšã³ã³ãŒããããã³ãŒããŠããã |
| å°æ° | æµ®åå°æ°ç¹ä»¥å€ã® 10 鲿° |
| IntPtr | ãã©ãããã©ãŒã å®çŸ©ãµã€ãºã®ãã€ã³ã¿ãŒãŸãã¯ãã³ãã« |
| ã¹ããªã³ã° | 0 åã1 ã€ä»¥äžã® Char ãªããžã§ã¯ãã®ã³ã¬ã¯ã·ã§ã³ |
次ã®è¡šã«ç€ºãçµã¿èŸŒã¿å㯠CLS æºæ ã§ã¯ãããŸããã
| éæºæ å | 説æ | CLS æºæ ã®ä»£æ¿ |
|---|---|---|
| SByte | 8 ããã笊å·ä»ãæŽæ°ããŒã¿å | Int16 |
| UInt16 | 16 ããã笊å·ãªãæŽæ° | Int32 |
| UInt32 | 32 ããã笊å·ãªãæŽæ° | Int64 |
| UInt64 | 64 ããã笊å·ãªãæŽæ° | Int64 (ãªãŒããŒãããŒããå¯èœæ§ããããŸã)ã BigIntegerããŸã㯠Double |
| UIntPtr | 笊å·ãªããã€ã³ã¿ãŒãŸãã¯ãã³ãã« | IntPtr |
.NET ã¯ã©ã¹ ã©ã€ãã©ãªãŸãã¯ãã®ä»ã®ã¯ã©ã¹ ã©ã€ãã©ãªã«ã¯ãCLS ã«æºæ ããŠããªãä»ã®åãå«ãŸããå ŽåããããŸããæ¬¡ã«äŸã瀺ããŸãã
ããã¯ã¹åãããå€åã æ¬¡ã® C# ã®äŸã§ã¯ã
int*ãšããååValueåã®ãããªã㯠ããããã£ãæã€ã¯ã©ã¹ãäœæããŸããint*ã¯ããã¯ã¹åãããå€åã§ãããããã³ã³ãã€ã©ã¯ CLS éæºæ ãšããŠãã©ã°ãèšå®ããŸããusing System; [assembly:CLSCompliant(true)] public unsafe class TestClass { private int* val; public TestClass(int number) { val = (int*) number; } public int* Value { get { return val; } } } // The compiler generates the following output when compiling this example: // warning CS3003: Type of 'TestClass.Value' is not CLS-compliantåæå®ãããåç §ããªããžã§ã¯ããžã®åç §ãšåãžã®åç §ãå«ãç¹æ®ãªã³ã³ã¹ãã©ã¯ãã§ãã åæå®ãããåç §ã¯ã TypedReference ã¯ã©ã¹ã«ãã£ãŠ .NET ã§è¡šãããŸãã
åã CLS ã«æºæ ããŠããªãå Žåã¯ãCLSCompliantAttribute屿§ãisCompliantã®falseå€ãšãšãã«é©çšããå¿
èŠããããŸãã 詳现ã«ã€ããŠã¯ãã CLSCompliantAttribute 屿§ ãã»ã¯ã·ã§ã³ãåç
§ããŠãã ããã
次ã®äŸã¯ãã¡ãœãã ã·ã°ããã£ãšãžã§ããªãã¯åã®ã€ã³ã¹ã¿ã³ã¹åã«ããã CLS æºæ ã®åé¡ã瀺ããŠããŸãã
InvoiceItemåã®ããããã£ãUInt32åã®ããããã£ãããã³Nullable<UInt32>åãšUInt32ã®ãã©ã¡ãŒã¿ãŒãæã€ã³ã³ã¹ãã©ã¯ã¿ãŒãæã€Nullable<UInt32> ã¯ã©ã¹ãå®çŸ©ããŸãã ãã®äŸãã³ã³ãã€ã«ããããšãããšã4 ã€ã®ã³ã³ãã€ã©èŠåã衚瀺ãããŸãã
using System;
[assembly: CLSCompliant(true)]
public class InvoiceItem
{
private uint invId = 0;
private uint itemId = 0;
private Nullable<uint> qty;
public InvoiceItem(uint sku, Nullable<uint> quantity)
{
itemId = sku;
qty = quantity;
}
public Nullable<uint> Quantity
{
get { return qty; }
set { qty = value; }
}
public uint InvoiceId
{
get { return invId; }
set { invId = value; }
}
}
// The attempt to compile the example displays the following output:
// Type1.cs(13,23): warning CS3001: Argument type 'uint' is not CLS-compliant
// Type1.cs(13,33): warning CS3001: Argument type 'uint?' is not CLS-compliant
// Type1.cs(19,26): warning CS3003: Type of 'InvoiceItem.Quantity' is not CLS-compliant
// Type1.cs(25,16): warning CS3003: Type of 'InvoiceItem.InvoiceId' is not CLS-compliant
<Assembly: CLSCompliant(True)>
Public Class InvoiceItem
Private invId As UInteger = 0
Private itemId As UInteger = 0
Private qty AS Nullable(Of UInteger)
Public Sub New(sku As UInteger, quantity As Nullable(Of UInteger))
itemId = sku
qty = quantity
End Sub
Public Property Quantity As Nullable(Of UInteger)
Get
Return qty
End Get
Set
qty = value
End Set
End Property
Public Property InvoiceId As UInteger
Get
Return invId
End Get
Set
invId = value
End Set
End Property
End Class
' The attempt to compile the example displays output similar to the following:
' Type1.vb(13) : warning BC40028: Type of parameter 'sku' is not CLS-compliant.
'
' Public Sub New(sku As UInteger, quantity As Nullable(Of UInteger))
' ~~~
' Type1.vb(13) : warning BC40041: Type 'UInteger' is not CLS-compliant.
'
' Public Sub New(sku As UInteger, quantity As Nullable(Of UInteger))
' ~~~~~~~~
' Type1.vb(18) : warning BC40041: Type 'UInteger' is not CLS-compliant.
'
' Public Property Quantity As Nullable(Of UInteger)
' ~~~~~~~~
' Type1.vb(27) : warning BC40027: Return type of function 'InvoiceId' is not CLS-compliant.
'
' Public Property InvoiceId As UInteger
' ~~~~~~~~~
ã³ã³ãã€ã©ã®èŠåãæé€ããã«ã¯ã InvoiceItem ãããªã㯠ã€ã³ã¿ãŒãã§ã€ã¹ã® CLS æºæ 以å€ã®åãæºæ åã«çœ®ãæããŸãã
using System;
[assembly: CLSCompliant(true)]
public class InvoiceItem
{
private uint invId = 0;
private uint itemId = 0;
private Nullable<int> qty;
public InvoiceItem(int sku, Nullable<int> quantity)
{
if (sku <= 0)
throw new ArgumentOutOfRangeException("The item number is zero or negative.");
itemId = (uint) sku;
qty = quantity;
}
public Nullable<int> Quantity
{
get { return qty; }
set { qty = value; }
}
public int InvoiceId
{
get { return (int) invId; }
set {
if (value <= 0)
throw new ArgumentOutOfRangeException("The invoice number is zero or negative.");
invId = (uint) value; }
}
}
<Assembly: CLSCompliant(True)>
Public Class InvoiceItem
Private invId As UInteger = 0
Private itemId As UInteger = 0
Private qty AS Nullable(Of Integer)
Public Sub New(sku As Integer, quantity As Nullable(Of Integer))
If sku <= 0 Then
Throw New ArgumentOutOfRangeException("The item number is zero or negative.")
End If
itemId = CUInt(sku)
qty = quantity
End Sub
Public Property Quantity As Nullable(Of Integer)
Get
Return qty
End Get
Set
qty = value
End Set
End Property
Public Property InvoiceId As Integer
Get
Return CInt(invId)
End Get
Set
invId = CUInt(value)
End Set
End Property
End Class
ãªã¹ããããŠããç¹å®ã®åã«å ããŠãäžéšã®ã«ããŽãªã®å㯠CLS ã«æºæ ããŠããŸããã ãããã«ã¯ãã¢ã³ãããŒãž ãã€ã³ã¿ãŒåãšé¢æ°ãã€ã³ã¿ãŒåãå«ãŸããŸãã æ¬¡ã®äŸã§ã¯ãæŽæ°ãžã®ãã€ã³ã¿ãŒã䜿çšããŠæŽæ°ã®é åãäœæãããããã³ã³ãã€ã©èŠåãçæãããŸãã
using System;
[assembly: CLSCompliant(true)]
public class ArrayHelper
{
unsafe public static Array CreateInstance(Type type, int* ptr, int items)
{
Array arr = Array.CreateInstance(type, items);
int* addr = ptr;
for (int ctr = 0; ctr < items; ctr++) {
int value = *addr;
arr.SetValue(value, ctr);
addr++;
}
return arr;
}
}
// The attempt to compile this example displays the following output:
// UnmanagedPtr1.cs(8,57): warning CS3001: Argument type 'int*' is not CLS-compliant
CLS æºæ ã®æœè±¡ã¯ã©ã¹ (ã€ãŸããC# ã§ abstract ãšããŠããŒã¯ãããŠããã¯ã©ã¹ããŸã㯠Visual Basic ã§ MustInherit ãšããŠããŒã¯ãããŠããã¯ã©ã¹) ã®å Žåãã¯ã©ã¹ã®ãã¹ãŠã®ã¡ã³ããŒã CLS ã«æºæ ããŠããå¿
èŠããããŸãã
ååä»ãèŠå
äžéšã®ããã°ã©ãã³ã°èšèªã¯å€§æåãšå°æåãåºå¥ãããã®ã§ãèå¥å (åå空éãåãã¡ã³ããŒã®ååãªã©) ã¯ã倧æåãšå°æåãéãã ãã§çžéããŸãã å°æåã®ãããã³ã°ãåãå Žåã2 ã€ã®èå¥åã¯åçãšèŠãªãããŸãã æ¬¡ã® C# ã®äŸã§ã¯ã Person ãš personã® 2 ã€ã®ãããªã㯠ã¯ã©ã¹ãå®çŸ©ããŸãã ãã®ã¯ã©ã¹ã¯å€§æåãšå°æåã®ã¿ãç°ãªãã®ã§ãã³ã³ãã€ã©ã¯ CLS éæºæ ãšããŠãã©ã°ãèšå®ããŸãã
using System;
[assembly: CLSCompliant(true)]
public class Person : person
{
}
public class person
{
}
// Compilation produces a compiler warning like the following:
// Naming1.cs(11,14): warning CS3005: Identifier 'person' differing
// only in case is not CLS-compliant
// Naming1.cs(6,14): (Location of symbol related to previous warning)
åå空éãåãã¡ã³ããŒã®ååãªã©ã®ããã°ã©ãã³ã°èšèªèå¥åã¯ã Unicode æšæºã«æºæ ããŠããå¿ èŠããããŸãã ããã¯ã次ã®ããšãæå³ããŸãã
èå¥åã®æåã®æåã«ã¯ãUnicode 倧æåãå°æåãã¿ã€ãã« ã±ãŒã¹æåã修食åæåããã®ä»ã®æåããŸãã¯æåçªå·ãæå®ã§ããŸãã Unicode æåã«ããŽãªã®è©³çްã«ã€ããŠã¯ã System.Globalization.UnicodeCategory åæåãåç §ããŠãã ããã
åŸç¶ã®æåã¯ãæåã®æåãšããŠä»»æã®ã«ããŽãªããååŸã§ããŸãããŸããéã¹ããŒã¹ ããŒã¯ãã¹ããŒã¹çµåããŒã¯ã10 鲿°ãã³ãã¯ã¿åºåãèšå·ãããã³æžåŒèšå®ã³ãŒããå«ããããšãã§ããŸãã
èå¥åãæ¯èŒããåã«ãæžåŒèšå®ã³ãŒãããã£ã«ã¿ãŒåŠçããèå¥åã Unicode æ£èŠåãã©ãŒã C ã«å€æããå¿
èŠããããŸããããã¯ã1 ã€ã®æåãè€æ°ã® UTF-16 ã§ãšã³ã³ãŒããããã³ãŒãåäœã§è¡šãããšãã§ããããã§ãã Unicode æ£èŠååœ¢åŒ C ã§åãã³ãŒãåäœãçæããæåã·ãŒã±ã³ã¹ã¯ãCLS ã«æºæ ããŠããŸããã æ¬¡ã®äŸã§ã¯ãæå Ã
NGSTROM SIGN (U+212B) ã§æ§æããã â« ãšããååã®ããããã£ãšãæå LATIN CAPITAL LETTER A WITH RING ABOVE (U+00C5) ã§æ§æããã Ã
ãšããååã®2ã€ç®ã®ããããã£ãå®çŸ©ããŸãã C# ã³ã³ãã€ã©ãš Visual Basic ã³ã³ãã€ã©ã®äž¡æ¹ã§ããœãŒã¹ ã³ãŒãã« CLS æºæ 以å€ã®ãã©ã°ãèšå®ãããŸãã
public class Size
{
private double a1;
private double a2;
public double â«
{
get { return a1; }
set { a1 = value; }
}
public double Ã
{
get { return a2; }
set { a2 = value; }
}
}
// Compilation produces a compiler warning like the following:
// Naming2a.cs(16,18): warning CS3005: Identifier 'Size.Ã
' differing only in case is not
// CLS-compliant
// Naming2a.cs(10,18): (Location of symbol related to previous warning)
// Naming2a.cs(18,8): warning CS3005: Identifier 'Size.Ã
.get' differing only in case is not
// CLS-compliant
// Naming2a.cs(12,8): (Location of symbol related to previous warning)
// Naming2a.cs(19,8): warning CS3005: Identifier 'Size.Ã
.set' differing only in case is not
// CLS-compliant
// Naming2a.cs(13,8): (Location of symbol related to previous warning)
<Assembly: CLSCompliant(True)>
Public Class Size
Private a1 As Double
Private a2 As Double
Public Property â« As Double
Get
Return a1
End Get
Set
a1 = value
End Set
End Property
Public Property Ã
As Double
Get
Return a2
End Get
Set
a2 = value
End Set
End Property
End Class
' Compilation produces a compiler warning like the following:
' Naming1.vb(9) : error BC30269: 'Public Property Ã
As Double' has multiple definitions
' with identical signatures.
'
' Public Property Ã
As Double
' ~
ç¹å®ã®ã¹ã³ãŒãå ã®ã¡ã³ããŒå (ã¢ã»ã³ããªå ã®åå空éãåå空éå ã®åãåå ã®ã¡ã³ããŒãªã©) ã¯ããªãŒããŒããŒãã«ãã£ãŠè§£æ±ºãããååãé€ããäžæã§ããå¿ èŠããããŸãã ãã®èŠä»¶ã¯å ±éåã·ã¹ãã ã®èŠä»¶ãããå³ãããã¹ã³ãŒãå ã®è€æ°ã®ã¡ã³ããŒãç°ãªãçš®é¡ã®ã¡ã³ããŒã§ããéããåãååãæã€ããšãã§ããŸã (ããšãã°ã1 ã€ã¯ã¡ãœããã§ã1 ã€ã¯ãã£ãŒã«ãã§ã)ã ç¹ã«ãåã¡ã³ããŒã®å Žå:
ãã£ãŒã«ããšå ¥ãåã«ãªã£ãåã¯ãååã ãã§èå¥ãããŸãã
åãååãæã€ã¡ãœãããããããã£ãããã³ã€ãã³ãã¯ãåãªãæ»ãå€ã®å以äžã®éããæã€å¿ èŠããããŸãã
次ã®äŸã¯ãã¡ã³ããŒåãã¹ã³ãŒãå
ã§äžæã§ããå¿
èŠãããèŠä»¶ã瀺ããŠããŸãã
Converterãšããååã® 4 ã€ã®ã¡ã³ããŒãå«ã Conversion ãšããååã®ã¯ã©ã¹ãå®çŸ©ããŸãã 3 ã€ã¯ã¡ãœããã§ããã1 ã€ã¯ããããã£ã§ãã
Int64 ãã©ã¡ãŒã¿ãŒãå«ãã¡ãœããã«ã¯äžæã®ååãä»ããããŸãããæ»ãå€ãã¡ã³ããŒã®ã·ã°ããã£ã®äžéšãšèŠãªãããªããããInt32 ãã©ã¡ãŒã¿ãŒãæã€ 2 ã€ã®ã¡ãœããã¯æå®ãããŸããã
Conversion ããããã£ã¯ããªãŒããŒããŒããããã¡ãœãããšåãååãæã€ããšãã§ããªãããããã®èŠä»¶ã«ãéåããŸãã
using System;
[assembly: CLSCompliant(true)]
public class Converter
{
public double Conversion(int number)
{
return (double) number;
}
public float Conversion(int number)
{
return (float) number;
}
public double Conversion(long number)
{
return (double) number;
}
public bool Conversion
{
get { return true; }
}
}
// Compilation produces a compiler error like the following:
// Naming3.cs(13,17): error CS0111: Type 'Converter' already defines a member called
// 'Conversion' with the same parameter types
// Naming3.cs(8,18): (Location of symbol related to previous error)
// Naming3.cs(23,16): error CS0102: The type 'Converter' already contains a definition for
// 'Conversion'
// Naming3.cs(8,18): (Location of symbol related to previous error)
<Assembly: CLSCompliant(True)>
Public Class Converter
Public Function Conversion(number As Integer) As Double
Return CDbl(number)
End Function
Public Function Conversion(number As Integer) As Single
Return CSng(number)
End Function
Public Function Conversion(number As Long) As Double
Return CDbl(number)
End Function
Public ReadOnly Property Conversion As Boolean
Get
Return True
End Get
End Property
End Class
' Compilation produces a compiler error like the following:
' Naming3.vb(8) : error BC30301: 'Public Function Conversion(number As Integer) As Double'
' and 'Public Function Conversion(number As Integer) As Single' cannot
' overload each other because they differ only by return types.
'
' Public Function Conversion(number As Integer) As Double
' ~~~~~~~~~~
' Naming3.vb(20) : error BC30260: 'Conversion' is already declared as 'Public Function
' Conversion(number As Integer) As Single' in this class.
'
' Public ReadOnly Property Conversion As Boolean
' ~~~~~~~~~~
åã
ã®èšèªã«ã¯äžæã®ããŒã¯ãŒããå«ãŸããŠãããããå
±éèšèªã©ã³ã¿ã€ã ã察象ãšããèšèªã¯ãããŒã¯ãŒããšäžèŽããèå¥å (ååãªã©) ãåç
§ããããã®ã¡ã«ããºã ãæäŸããå¿
èŠããããŸãã ããšãã°ã case 㯠C# ãš Visual Basic ã®äž¡æ¹ã®ããŒã¯ãŒãã§ãã ãã ããæ¬¡ã® Visual Basic ã³ãŒãäŸã§ã¯ãå·Šå³ã®äžãã£ãã䜿çšããŠãcase ããŒã¯ãŒããš case ãšããååã®ã¯ã©ã¹ãæç¢ºã«åºå¥ã§ããŸãã ãã以å€ã®å Žåããã®äŸã§ã¯"ããŒã¯ãŒãã¯èå¥åãšããŠæå¹ã§ã¯ãããŸãã" ãšãããšã©ãŒ ã¡ãã»ãŒãžãçæãããã³ã³ãã€ã«ã«å€±æããŸãã
Public Class [case]
Private _id As Guid
Private name As String
Public Sub New(name As String)
_id = Guid.NewGuid()
Me.name = name
End Sub
Public ReadOnly Property ClientName As String
Get
Return name
End Get
End Property
End Class
次㮠C# ã®äŸã§ã¯ãcase ã·ã³ãã«ã䜿çšããŠèšèªããŒã¯ãŒãããèå¥åãåºå¥ããããšã§ã@ ã¯ã©ã¹ãã€ã³ã¹ã¿ã³ã¹åã§ããŸãã ããã䜿çšããªãå ŽåãC# ã³ã³ãã€ã©ã§ã¯ã"Type expected" ãš "Invalid expression term 'case' ãšãã 2 ã€ã®ãšã©ãŒ ã¡ãã»ãŒãžã衚瀺ãããŸãã
using System;
public class Example
{
public static void Main()
{
@case c = new @case("John");
Console.WriteLine(c.ClientName);
}
}
å倿
å ±éèšèªä»æ§ã§ã¯ã次㮠2 ã€ã®å€ææŒç®åãå®çŸ©ãããŠããŸãã
op_Implicitã¯ãããŒã¿ã粟床ã倱ããªãæ¡å€§å€æã«äœ¿çšãããŸãã ããšãã°ãDecimalæ§é äœã«ã¯ãæŽæ°åã®å€ãšop_Implicitå€ãCharå€ã«å€æãããªãŒããŒããŒããããDecimalæŒç®åãå«ãŸããŠããŸããop_Explicitã¯ãç¯å²ã粟床ãå°ããå€ãžã®å€æãè¡ããçž®å°å€æãã«äœ¿çšãããããã«ãã倧ããã粟床ã倱ãããå¯èœæ§ããããŸãã ããšãã°ãDecimalæ§é äœã«ã¯ãop_Explicitå€ãšDoubleå€ãSingleã«å€æããDecimalå€ãæŽæ°å€ãDecimalãDoubleãããã³Singleã«å€æãããªãŒããŒããŒããããCharæŒç®åãå«ãŸããŠããŸãã
ãã ãããã¹ãŠã®èšèªã§æŒç®åã®ãªãŒããŒããŒããã«ã¹ã¿ã æŒç®åã®å®çŸ©ããµããŒããããŠããããã§ã¯ãããŸããã ãããã®å€ææŒç®åãå®è£
ããå Žåã¯ã倿ãå®è¡ããå¥ã®æ¹æ³ãæäŸããå¿
èŠããããŸãã
From
Xxx ã¡ãœãããš ToXxx ã¡ãœãããæå®ããããšããå§ãããŸãã
次ã®äŸã§ã¯ãCLS æºæ ã®æé»çããã³æç€ºçãªå€æãå®çŸ©ããŸãã 笊å·ãªãå粟床浮åå°æ°ç¹æ°ã衚ã UDouble ã¯ã©ã¹ãäœæãããŸãã
UDoubleããDoubleãžã®æé»çãªå€æãããã³UDoubleããSingleãžã®æç€ºçãªå€æãDoubleããUDoubleãžã®Singleãããã³UDoubleãžã®æç€ºçãªå€æãæäŸãããŸãã ãŸããæé»çãªå€ææŒç®åã®ä»£ããã« ToDouble ã¡ãœãããå®çŸ©ããæç€ºçãªå€ææŒç®åã®ä»£ããã« ToSingleã FromDoubleãããã³ FromSingle ã¡ãœãããå®çŸ©ããŸãã
using System;
public struct UDouble
{
private double number;
public UDouble(double value)
{
if (value < 0)
throw new InvalidCastException("A negative value cannot be converted to a UDouble.");
number = value;
}
public UDouble(float value)
{
if (value < 0)
throw new InvalidCastException("A negative value cannot be converted to a UDouble.");
number = value;
}
public static readonly UDouble MinValue = (UDouble) 0.0;
public static readonly UDouble MaxValue = (UDouble) Double.MaxValue;
public static explicit operator Double(UDouble value)
{
return value.number;
}
public static implicit operator Single(UDouble value)
{
if (value.number > (double) Single.MaxValue)
throw new InvalidCastException("A UDouble value is out of range of the Single type.");
return (float) value.number;
}
public static explicit operator UDouble(double value)
{
if (value < 0)
throw new InvalidCastException("A negative value cannot be converted to a UDouble.");
return new UDouble(value);
}
public static implicit operator UDouble(float value)
{
if (value < 0)
throw new InvalidCastException("A negative value cannot be converted to a UDouble.");
return new UDouble(value);
}
public static Double ToDouble(UDouble value)
{
return (Double) value;
}
public static float ToSingle(UDouble value)
{
return (float) value;
}
public static UDouble FromDouble(double value)
{
return new UDouble(value);
}
public static UDouble FromSingle(float value)
{
return new UDouble(value);
}
}
Public Structure UDouble
Private number As Double
Public Sub New(value As Double)
If value < 0 Then
Throw New InvalidCastException("A negative value cannot be converted to a UDouble.")
End If
number = value
End Sub
Public Sub New(value As Single)
If value < 0 Then
Throw New InvalidCastException("A negative value cannot be converted to a UDouble.")
End If
number = value
End Sub
Public Shared ReadOnly MinValue As UDouble = CType(0.0, UDouble)
Public Shared ReadOnly MaxValue As UDouble = Double.MaxValue
Public Shared Widening Operator CType(value As UDouble) As Double
Return value.number
End Operator
Public Shared Narrowing Operator CType(value As UDouble) As Single
If value.number > CDbl(Single.MaxValue) Then
Throw New InvalidCastException("A UDouble value is out of range of the Single type.")
End If
Return CSng(value.number)
End Operator
Public Shared Narrowing Operator CType(value As Double) As UDouble
If value < 0 Then
Throw New InvalidCastException("A negative value cannot be converted to a UDouble.")
End If
Return New UDouble(value)
End Operator
Public Shared Narrowing Operator CType(value As Single) As UDouble
If value < 0 Then
Throw New InvalidCastException("A negative value cannot be converted to a UDouble.")
End If
Return New UDouble(value)
End Operator
Public Shared Function ToDouble(value As UDouble) As Double
Return CType(value, Double)
End Function
Public Shared Function ToSingle(value As UDouble) As Single
Return CType(value, Single)
End Function
Public Shared Function FromDouble(value As Double) As UDouble
Return New UDouble(value)
End Function
Public Shared Function FromSingle(value As Single) As UDouble
Return New UDouble(value)
End Function
End Structure
é å
CLS æºæ ã®é åã¯ã次ã®èŠåã«æºæ ããŠããŸãã
é åã®ãã¹ãŠã®æ¬¡å ã®äžé㯠0 ã§ããå¿ èŠããããŸãã æ¬¡ã®äŸã§ã¯ãäžéã 1 ã® CLS æºæ ã§ãªãé åãäœæããŸãã CLSCompliantAttribute屿§ãååšããã«ãããããããã³ã³ãã€ã©ã¯ã
Numbers.GetTenPrimesã¡ãœããã«ãã£ãŠè¿ãããé åã CLS ã«æºæ ããŠããªãããšãæ€åºããŸããã[assembly: CLSCompliant(true)] public class Numbers { public static Array GetTenPrimes() { Array arr = Array.CreateInstance(typeof(Int32), new int[] {10}, new int[] {1}); arr.SetValue(1, 1); arr.SetValue(2, 2); arr.SetValue(3, 3); arr.SetValue(5, 4); arr.SetValue(7, 5); arr.SetValue(11, 6); arr.SetValue(13, 7); arr.SetValue(17, 8); arr.SetValue(19, 9); arr.SetValue(23, 10); return arr; } }<Assembly: CLSCompliant(True)> Public Class Numbers Public Shared Function GetTenPrimes() As Array Dim arr As Array = Array.CreateInstance(GetType(Int32), {10}, {1}) arr.SetValue(1, 1) arr.SetValue(2, 2) arr.SetValue(3, 3) arr.SetValue(5, 4) arr.SetValue(7, 5) arr.SetValue(11, 6) arr.SetValue(13, 7) arr.SetValue(17, 8) arr.SetValue(19, 9) arr.SetValue(23, 10) Return arr End Function End Classãã¹ãŠã®é åèŠçŽ ã¯ãCLS ã«æºæ ããåã§æ§æãããŠããå¿ èŠããããŸãã æ¬¡ã®äŸã§ã¯ãCLS ã«æºæ ããŠããªãé åãè¿ã 2 ã€ã®ã¡ãœãããå®çŸ©ããŸãã 1 ã€ç®ã¯ã UInt32 å€ã®é åãè¿ããŸãã 2 ã€ç®ã¯ãObjectå€ãšInt32å€ãå«ãUInt32é åãè¿ããŸãã ã³ã³ãã€ã©ã¯ã UInt32 åã®ããã«æåã®é åãéæºæ ãšããŠèå¥ããŸããã2 çªç®ã®é åã« CLS æºæ 以å€ã®èŠçŽ ãå«ãŸããŠããããšãèªèã§ããŸããã
using System; [assembly: CLSCompliant(true)] public class Numbers { public static UInt32[] GetTenPrimes() { uint[] arr = { 1u, 2u, 3u, 5u, 7u, 11u, 13u, 17u, 19u }; return arr; } public static Object[] GetFivePrimes() { Object[] arr = { 1, 2, 3, 5u, 7u }; return arr; } } // Compilation produces a compiler warning like the following: // Array2.cs(8,27): warning CS3002: Return type of 'Numbers.GetTenPrimes()' is not // CLS-compliant<Assembly: CLSCompliant(True)> Public Class Numbers Public Shared Function GetTenPrimes() As UInt32() Return {1ui, 2ui, 3ui, 5ui, 7ui, 11ui, 13ui, 17ui, 19ui} End Function Public Shared Function GetFivePrimes() As Object() Dim arr() As Object = {1, 2, 3, 5ui, 7ui} Return arr End Function End Class ' Compilation produces a compiler warning like the following: ' warning BC40027: Return type of function 'GetTenPrimes' is not CLS-compliant. ' ' Public Shared Function GetTenPrimes() As UInt32() ' ~~~~~~~~~~~~é åãã©ã¡ãŒã¿ãŒãæã€ã¡ãœããã®ãªãŒããŒããŒã解決ã¯ãããããé åã§ãããšããäºå®ãšãã®èŠçŽ åã«åºã¥ããŠããŸãã ãã®ããããªãŒããŒããŒãããã
GetSquaresã¡ãœããã®æ¬¡ã®å®çŸ©ã¯ CLS ã«æºæ ããŠããŸããusing System; using System.Numerics; [assembly: CLSCompliant(true)] public class Numbers { public static byte[] GetSquares(byte[] numbers) { byte[] numbersOut = new byte[numbers.Length]; for (int ctr = 0; ctr < numbers.Length; ctr++) { int square = ((int) numbers[ctr]) * ((int) numbers[ctr]); if (square <= Byte.MaxValue) numbersOut[ctr] = (byte) square; // If there's an overflow, assign MaxValue to the corresponding // element. else numbersOut[ctr] = Byte.MaxValue; } return numbersOut; } public static BigInteger[] GetSquares(BigInteger[] numbers) { BigInteger[] numbersOut = new BigInteger[numbers.Length]; for (int ctr = 0; ctr < numbers.Length; ctr++) numbersOut[ctr] = numbers[ctr] * numbers[ctr]; return numbersOut; } }Imports System.Numerics <Assembly: CLSCompliant(True)> Public Module Numbers Public Function GetSquares(numbers As Byte()) As Byte() Dim numbersOut(numbers.Length - 1) As Byte For ctr As Integer = 0 To numbers.Length - 1 Dim square As Integer = (CInt(numbers(ctr)) * CInt(numbers(ctr))) If square <= Byte.MaxValue Then numbersOut(ctr) = CByte(square) ' If there's an overflow, assign MaxValue to the corresponding ' element. Else numbersOut(ctr) = Byte.MaxValue End If Next Return numbersOut End Function Public Function GetSquares(numbers As BigInteger()) As BigInteger() Dim numbersOut(numbers.Length - 1) As BigInteger For ctr As Integer = 0 To numbers.Length - 1 numbersOut(ctr) = numbers(ctr) * numbers(ctr) Next Return numbersOut End Function End Module
ã€ã³ã¿ãŒãã§ã€ã¹
CLS æºæ ã€ã³ã¿ãŒãã§ã€ã¹ã§ã¯ãããããã£ãã€ãã³ããããã³ä»®æ³ã¡ãœãã (å®è£ ã®ãªãã¡ãœãã) ãå®çŸ©ã§ããŸãã CLS æºæ ã€ã³ã¿ãŒãã§ã€ã¹ã«ã¯ã次ã®ãããããæå®ããããšã¯ã§ããŸããã
éçã¡ãœãããŸãã¯éçãã£ãŒã«ãã ã€ã³ã¿ãŒãã§ã€ã¹ã§éçã¡ã³ããŒãå®çŸ©ãããšãC# ã³ã³ãã€ã©ãš Visual Basic ã³ã³ãã€ã©ã®äž¡æ¹ã§ã³ã³ãã€ã© ãšã©ãŒãçæãããŸãã
ãã£ãŒã«ãã ã€ã³ã¿ãŒãã§ã€ã¹ã§ãã£ãŒã«ããå®çŸ©ãããšãC# ã³ã³ãã€ã©ãš Visual Basic ã³ã³ãã€ã©ã®äž¡æ¹ã§ã³ã³ãã€ã© ãšã©ãŒãçæãããŸãã
CLS ã«æºæ ããŠããªãã¡ãœããã ããšãã°ã次ã®ã€ã³ã¿ãŒãã§ã€ã¹å®çŸ©ã«ã¯ãCLS éæºæ ãšããŠããŒã¯ãããŠããã¡ãœãã (
INumber.GetUnsigned) ãå«ãŸããŠããŸãã ãã®äŸã§ã¯ãã³ã³ãã€ã©èŠåãçæãããŸããusing System; [assembly:CLSCompliant(true)] public interface INumber { int Length(); [CLSCompliant(false)] ulong GetUnsigned(); } // Attempting to compile the example displays output like the following: // Interface2.cs(8,32): warning CS3010: 'INumber.GetUnsigned()': CLS-compliant interfaces // must have only CLS-compliant members<Assembly: CLSCompliant(True)> Public Interface INumber Function Length As Integer <CLSCompliant(False)> Function GetUnsigned As ULong End Interface ' Attempting to compile the example displays output like the following: ' Interface2.vb(9) : warning BC40033: Non CLS-compliant 'function' is not allowed in a ' CLS-compliant interface. ' ' <CLSCompliant(False)> Function GetUnsigned As ULong ' ~~~~~~~~~~~ãã®èŠåã«ãããCLS æºæ ã§ãªãã¡ã³ããŒãå®è£ ããããã« CLS æºæ åã¯å¿ èŠãããŸããã CLS æºæ ãã¬ãŒã ã¯ãŒã¯ããCLS æºæ ã§ãªãã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ããã¯ã©ã¹ãå ¬éããå Žåã¯ãCLS ã«æºæ ããŠããªããã¹ãŠã®ã¡ã³ããŒã®å ·è±¡å®è£ ãæäŸããå¿ èŠããããŸãã
CLS æºæ èšèªã³ã³ãã€ã©ã§ã¯ãã¯ã©ã¹ãè€æ°ã®ã€ã³ã¿ãŒãã§ã€ã¹ã§åãååãšã·ã°ããã£ãæã€ã¡ã³ããŒã®åå¥ã®å®è£
ãæäŸã§ããããã«ããå¿
èŠããããŸãã C# ãš Visual Basic ã®äž¡æ¹ã§ãåãååã®ã¡ãœããã®ããŸããŸãªå®è£
ãæäŸãã æç€ºçãªã€ã³ã¿ãŒãã§ã€ã¹ å®è£
ããµããŒããããŠããŸãã Visual Basic ã§ã¯ã Implements ããŒã¯ãŒãããµããŒããããŠããŸããããã«ãããç¹å®ã®ã¡ã³ããŒãå®è£
ããã€ã³ã¿ãŒãã§ã€ã¹ãšã¡ã³ããŒãæç€ºçã«æå®ã§ããŸãã æ¬¡ã®äŸã§ã¯ãTemperatureã€ã³ã¿ãŒãã§ã€ã¹ãšICelsius ã€ã³ã¿ãŒãã§ã€ã¹ãæç€ºçãªã€ã³ã¿ãŒãã§ã€ã¹å®è£
ãšããŠå®è£
ããIFahrenheit ã¯ã©ã¹ãå®çŸ©ããããšã§ããã®ã·ããªãªã瀺ããŸãã
using System;
[assembly: CLSCompliant(true)]
public interface IFahrenheit
{
decimal GetTemperature();
}
public interface ICelsius
{
decimal GetTemperature();
}
public class Temperature : ICelsius, IFahrenheit
{
private decimal _value;
public Temperature(decimal value)
{
// We assume that this is the Celsius value.
_value = value;
}
decimal IFahrenheit.GetTemperature()
{
return _value * 9 / 5 + 32;
}
decimal ICelsius.GetTemperature()
{
return _value;
}
}
public class Example
{
public static void Main()
{
Temperature temp = new Temperature(100.0m);
ICelsius cTemp = temp;
IFahrenheit fTemp = temp;
Console.WriteLine($"Temperature in Celsius: {cTemp.GetTemperature()} degrees");
Console.WriteLine($"Temperature in Fahrenheit: {fTemp.GetTemperature()} degrees");
}
}
// The example displays the following output:
// Temperature in Celsius: 100.0 degrees
// Temperature in Fahrenheit: 212.0 degrees
<Assembly: CLSCompliant(True)>
Public Interface IFahrenheit
Function GetTemperature() As Decimal
End Interface
Public Interface ICelsius
Function GetTemperature() As Decimal
End Interface
Public Class Temperature : Implements ICelsius, IFahrenheit
Private _value As Decimal
Public Sub New(value As Decimal)
' We assume that this is the Celsius value.
_value = value
End Sub
Public Function GetFahrenheit() As Decimal _
Implements IFahrenheit.GetTemperature
Return _value * 9 / 5 + 32
End Function
Public Function GetCelsius() As Decimal _
Implements ICelsius.GetTemperature
Return _value
End Function
End Class
Module Example
Public Sub Main()
Dim temp As New Temperature(100.0d)
Console.WriteLine("Temperature in Celsius: {0} degrees",
temp.GetCelsius())
Console.WriteLine("Temperature in Fahrenheit: {0} degrees",
temp.GetFahrenheit())
End Sub
End Module
' The example displays the following output:
' Temperature in Celsius: 100.0 degrees
' Temperature in Fahrenheit: 212.0 degrees
åæå
CLS æºæ ã®åæåã¯ã次ã®èŠåã«åŸãå¿ èŠããããŸãã
åæåã®åºã«ãªãåã¯ãCLS æºæ ã®çµã¿èŸŒã¿æŽæ° (Byteã Int16ã Int32ããŸã㯠Int64) ã§ããå¿ èŠããããŸãã ããšãã°ã次ã®ã³ãŒãã¯ãåºã«ãªãåã UInt32 åæäœãå®çŸ©ããããšè©Šã¿ãã³ã³ãã€ã©èŠåãçæããŸãã
using System; [assembly: CLSCompliant(true)] public enum Size : uint { Unspecified = 0, XSmall = 1, Small = 2, Medium = 3, Large = 4, XLarge = 5 }; public class Clothing { public string Name; public string Type; public string Size; } // The attempt to compile the example displays a compiler warning like the following: // Enum3.cs(6,13): warning CS3009: 'Size': base type 'uint' is not CLS-compliant<Assembly: CLSCompliant(True)> Public Enum Size As UInt32 Unspecified = 0 XSmall = 1 Small = 2 Medium = 3 Large = 4 XLarge = 5 End Enum Public Class Clothing Public Name As String Public Type As String Public Size As Size End Class ' The attempt to compile the example displays a compiler warning like the following: ' Enum3.vb(6) : warning BC40032: Underlying type 'UInt32' of Enum is not CLS-compliant. ' ' Public Enum Size As UInt32 ' ~~~~åæåã«ã¯ã
Value__屿§ã§ããŒã¯ããã FieldAttributes.RTSpecialName ãšããååã®åäžã®ã€ã³ã¹ã¿ã³ã¹ ãã£ãŒã«ããå¿ èŠã§ãã ããã«ããããã£ãŒã«ãå€ãæé»çã«åç §ã§ããŸããåæåã«ã¯ãåæåèªäœã®åãšäžèŽããåãæã€ãªãã©ã«éçãã£ãŒã«ããå«ãŸããŸãã ããšãã°ã
StateãšState.Onã®å€ãæã€State.Offåæäœãå®çŸ©ããå ŽåãState.OnãšState.Offã¯ã©ã¡ãããåãStateãªãã©ã«ã®éçãã£ãŒã«ãã§ããåæåã«ã¯æ¬¡ã® 2 çš®é¡ããããŸãã
çžäºã«æä»çãªååä»ãæŽæ°å€ã®ã»ããã衚ãåæäœã ãã®çš®é¡ã®åæåã¯ãã«ã¹ã¿ã 屿§ã System.FlagsAttribute ãããŠããªãããšã§ç€ºãããŸãã
ååã®ãªãå€ãçæããããã«çµã¿åãããããšãã§ããããã ãã©ã°ã®ã»ããã衚ãåæäœã ãã®çš®é¡ã®åæåã¯ã System.FlagsAttribute ã«ã¹ã¿ã 屿§ã®ååšã«ãã£ãŠç€ºãããŸãã
詳现ã«ã€ããŠã¯ã Enum æ§é ã®ããã¥ã¡ã³ããåç §ããŠãã ããã
åæäœã®å€ã¯ãæå®ããå€ã®ç¯å²ã«éå®ãããŸããã ã€ãŸããåæåã®å€ã®ç¯å²ã¯ãåºã«ãªãå€ã®ç¯å²ã§ãã Enum.IsDefined ã¡ãœããã䜿çšããŠãæå®ããå€ãåæäœã®ã¡ã³ããŒã§ãããã©ããã倿ã§ããŸãã
äžè¬çãªåã¡ã³ããŒ
å ±éèšèªä»æ§ã§ã¯ããã¹ãŠã®ãã£ãŒã«ããšã¡ãœããã«ç¹å®ã®ã¯ã©ã¹ã®ã¡ã³ããŒãšããŠã¢ã¯ã»ã¹ããå¿ èŠããããŸãã ãããã£ãŠãã°ããŒãã«éçãã£ãŒã«ããšã¡ãœãã (ã€ãŸããåãšã¯å¥ã«å®çŸ©ãããéçãã£ãŒã«ããŸãã¯ã¡ãœãã) 㯠CLS ã«æºæ ããŠããŸããã ãœãŒã¹ ã³ãŒãã«ã°ããŒãã« ãã£ãŒã«ããŸãã¯ã¡ãœãããå«ããå ŽåãC# ã³ã³ãã€ã©ãš Visual Basic ã³ã³ãã€ã©ã®äž¡æ¹ã§ã³ã³ãã€ã© ãšã©ãŒãçæãããŸãã
å
±éèšèªä»æ§ã§ã¯ãæšæºã®ãããŒãžãåŒã³åºãèŠåã®ã¿ããµããŒããããŠããŸãã
varargs ããŒã¯ãŒãã§ããŒã¯ãããå¯å€åŒæ°ãªã¹ããæã€ã¢ã³ãããŒãžåŒã³åºãèŠåãšã¡ãœããã¯ãµããŒããããŠããŸããã æšæºã®ãããŒãžãåŒã³åºãèŠåãšäºææ§ã®ããå¯å€åŒæ°ãªã¹ãã®å Žåã¯ã ParamArrayAttribute 屿§ãŸãã¯åã
ã®èšèªã®å®è£
(C# ã® params ããŒã¯ãŒãã Visual Basic ã® ParamArray ããŒã¯ãŒããªã©) ã䜿çšããŸãã
ã¡ã³ããŒã®ã¢ã¯ã»ã·ããªãã£
ç¶æ¿ãããã¡ã³ããŒããªãŒããŒã©ã€ãããŠãããã®ã¡ã³ããŒã®ã¢ã¯ã»ã·ããªãã£ã倿Žããããšã¯ã§ããŸããã ããšãã°ãåºåºã¯ã©ã¹ã®ãããªã㯠ã¡ãœããã¯ã掟çã¯ã©ã¹ã®ãã©ã€ããŒã ã¡ãœããã§ã¯ãªãŒããŒã©ã€ãã§ããŸããã 1ã€ã®äŸå€ããããŸããããã¯ãå¥ã®ã¢ã»ã³ããªã®åã«ãã£ãŠãªãŒããŒã©ã€ãããããããã¢ã»ã³ããªã®protected internal (C#ã®å Žå) ãŸãã¯Protected Friend (Visual Basicã®å Žå) ã¡ã³ããŒã§ãã ãã®å ŽåããªãŒããŒã©ã€ãã®ã¢ã¯ã»ã·ããªãã£ã¯ Protectedã
次ã®äŸã¯ãCLSCompliantAttribute 屿§ã true ã«èšå®ãããHumanããæŽŸçããã¯ã©ã¹ã§ãã Animal ããSpecies ããããã£ã®ã¢ã¯ã»ã·ããªãã£ããããªãã¯ãããã©ã€ããŒãã«å€æŽããããšãããšãã«çæããããšã©ãŒã瀺ããŠããŸãã ãã®äŸã§ã¯ãã¢ã¯ã»ã·ããªãã£ããããªãã¯ã«å€æŽããããšãæ£åžžã«ã³ã³ãã€ã«ãããŸãã
using System;
[assembly: CLSCompliant(true)]
public class Animal
{
private string _species;
public Animal(string species)
{
_species = species;
}
public virtual string Species
{
get { return _species; }
}
public override string ToString()
{
return _species;
}
}
public class Human : Animal
{
private string _name;
public Human(string name) : base("Homo Sapiens")
{
_name = name;
}
public string Name
{
get { return _name; }
}
private override string Species
{
get { return base.Species; }
}
public override string ToString()
{
return _name;
}
}
public class Example
{
public static void Main()
{
Human p = new Human("John");
Console.WriteLine(p.Species);
Console.WriteLine(p.ToString());
}
}
// The example displays the following output:
// error CS0621: 'Human.Species': virtual or abstract members cannot be private
<Assembly: CLSCompliant(True)>
Public Class Animal
Private _species As String
Public Sub New(species As String)
_species = species
End Sub
Public Overridable ReadOnly Property Species As String
Get
Return _species
End Get
End Property
Public Overrides Function ToString() As String
Return _species
End Function
End Class
Public Class Human : Inherits Animal
Private _name As String
Public Sub New(name As String)
MyBase.New("Homo Sapiens")
_name = name
End Sub
Public ReadOnly Property Name As String
Get
Return _name
End Get
End Property
Private Overrides ReadOnly Property Species As String
Get
Return MyBase.Species
End Get
End Property
Public Overrides Function ToString() As String
Return _name
End Function
End Class
Public Module Example
Public Sub Main()
Dim p As New Human("John")
Console.WriteLine(p.Species)
Console.WriteLine(p.ToString())
End Sub
End Module
' The example displays the following output:
' 'Private Overrides ReadOnly Property Species As String' cannot override
' 'Public Overridable ReadOnly Property Species As String' because
' they have different access levels.
'
' Private Overrides ReadOnly Property Species As String
ã¡ã³ããŒã®ã·ã°ããã£å
ã®åã¯ããã®ã¡ã³ããŒãã¢ã¯ã»ã¹å¯èœãªå Žåã¯åžžã«ã¢ã¯ã»ã¹å¯èœã§ããå¿
èŠããããŸãã ããšãã°ããããªã㯠ã¡ã³ããŒã¯ãåããã©ã€ããŒããä¿è·ããŸãã¯å
éšã§ãããã©ã¡ãŒã¿ãŒãå«ããããšã¯ã§ããŸããã æ¬¡ã®äŸã¯ã StringWrapper ã¯ã©ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒããæååå€ã®ã©ããæ¹æ³ã決å®ããå
éš StringOperationType åæå€ãå
¬éãããšãã«çºçããã³ã³ãã€ã© ãšã©ãŒã瀺ããŠããŸãã
using System;
using System.Text;
public class StringWrapper
{
string internalString;
StringBuilder internalSB = null;
bool useSB = false;
public StringWrapper(StringOperationType type)
{
if (type == StringOperationType.Normal) {
useSB = false;
}
else {
useSB = true;
internalSB = new StringBuilder();
}
}
// The remaining source code...
}
internal enum StringOperationType { Normal, Dynamic }
// The attempt to compile the example displays the following output:
// error CS0051: Inconsistent accessibility: parameter type
// 'StringOperationType' is less accessible than method
// 'StringWrapper.StringWrapper(StringOperationType)'
Imports System.Text
<Assembly: CLSCompliant(True)>
Public Class StringWrapper
Dim internalString As String
Dim internalSB As StringBuilder = Nothing
Dim useSB As Boolean = False
Public Sub New(type As StringOperationType)
If type = StringOperationType.Normal Then
useSB = False
Else
internalSB = New StringBuilder()
useSB = True
End If
End Sub
' The remaining source code...
End Class
Friend Enum StringOperationType As Integer
Normal = 0
Dynamic = 1
End Enum
' The attempt to compile the example displays the following output:
' error BC30909: 'type' cannot expose type 'StringOperationType'
' outside the project through class 'StringWrapper'.
'
' Public Sub New(type As StringOperationType)
' ~~~~~~~~~~~~~~~~~~~
ãžã§ããªãã¯åãšã¡ã³ããŒ
å ¥ãåã«ãªã£ãåã«ã¯ãå€åŽã®åãšåæ°ä»¥äžã®ãžã§ããªã㯠ãã©ã¡ãŒã¿ãŒãåžžã«å«ãŸããŸãã ãããã¯å²ã¿åã®ãžã§ããªãã¯ãã©ã¡ãŒã¿ãŒãšäœçœ®çã«å¯Ÿå¿ããŠããŸãã ãžã§ããªãã¯åã«ã¯ãæ°ãããžã§ããªã㯠ãã©ã¡ãŒã¿ãŒãå«ããããšãã§ããŸãã
å«ãåã®ãžã§ããªãã¯åãã©ã¡ãŒã¿ãŒãšãã®å
¥ãåã«ãªã£ãåã®é¢ä¿ã¯ãåã
ã®èšèªã®æ§æã«ãã£ãŠé衚瀺ã«ãªãå ŽåããããŸãã æ¬¡ã®äŸã§ã¯ããžã§ããªãã¯å Outer<T> ã«ã¯ã Inner1A ãš Inner1B<U>ã® 2 ã€ã®å
¥ãåã«ãªã£ãã¯ã©ã¹ãå«ãŸããŠããŸãã åã¯ã©ã¹ãToStringããç¶æ¿ããObject.ToString() ã¡ãœããã®åŒã³åºãã¯ãå
¥ãåã«ãªã£ãåã¯ã©ã¹ã«ããã®å
å«ã¯ã©ã¹ã®åãã©ã¡ãŒã¿ãŒãå«ãŸããŠããããšã瀺ããŠããŸãã
using System;
[assembly:CLSCompliant(true)]
public class Outer<T>
{
T value;
public Outer(T value)
{
this.value = value;
}
public class Inner1A : Outer<T>
{
public Inner1A(T value) : base(value)
{ }
}
public class Inner1B<U> : Outer<T>
{
U value2;
public Inner1B(T value1, U value2) : base(value1)
{
this.value2 = value2;
}
}
}
public class Example
{
public static void Main()
{
var inst1 = new Outer<String>("This");
Console.WriteLine(inst1);
var inst2 = new Outer<String>.Inner1A("Another");
Console.WriteLine(inst2);
var inst3 = new Outer<String>.Inner1B<int>("That", 2);
Console.WriteLine(inst3);
}
}
// The example displays the following output:
// Outer`1[System.String]
// Outer`1+Inner1A[System.String]
// Outer`1+Inner1B`1[System.String,System.Int32]
<Assembly: CLSCompliant(True)>
Public Class Outer(Of T)
Dim value As T
Public Sub New(value As T)
Me.value = value
End Sub
Public Class Inner1A : Inherits Outer(Of T)
Public Sub New(value As T)
MyBase.New(value)
End Sub
End Class
Public Class Inner1B(Of U) : Inherits Outer(Of T)
Dim value2 As U
Public Sub New(value1 As T, value2 As U)
MyBase.New(value1)
Me.value2 = value2
End Sub
End Class
End Class
Public Module Example
Public Sub Main()
Dim inst1 As New Outer(Of String)("This")
Console.WriteLine(inst1)
Dim inst2 As New Outer(Of String).Inner1A("Another")
Console.WriteLine(inst2)
Dim inst3 As New Outer(Of String).Inner1B(Of Integer)("That", 2)
Console.WriteLine(inst3)
End Sub
End Module
' The example displays the following output:
' Outer`1[System.String]
' Outer`1+Inner1A[System.String]
' Outer`1+Inner1B`1[System.String,System.Int32]
ãžã§ããªãã¯ååã¯ãã©ãŒã å'n ã§ãšã³ã³ãŒããããŸãã name ã¯ååã' ã¯æåãªãã©ã«ã n ã¯åã§å®£èšããããã©ã¡ãŒã¿ãŒã®æ°ããŸãã¯å ¥ãåã«ãªã£ããžã§ããªãã¯åã®å Žåã¯ãæ°ããå°å ¥ãããåãã©ã¡ãŒã¿ãŒã®æ°ã§ãã ãžã§ããªãã¯ååã®ãã®ãšã³ã³ãŒãã¯ãäž»ã«ãªãã¬ã¯ã·ã§ã³ã䜿çšããŠã©ã€ãã©ãªå ã® CLS æºæ ã®ãžã§ããªãã¯åã«ã¢ã¯ã»ã¹ããéçºè ã«ãšã£ãŠéèŠã§ãã
å¶çŽããžã§ããªãã¯åã«é©çšãããå Žåãå¶çŽãšããŠäœ¿çšãããåã CLS ã«æºæ ããŠããå¿
èŠããããŸãã æ¬¡ã®äŸã§ã¯ãCLS ã«æºæ ããŠããªã BaseClass ãšããååã®ã¯ã©ã¹ãšãåãã©ã¡ãŒã¿ãŒã BaseCollection ããæŽŸçããå¿
èŠããã BaseClass ãšããååã®ãžã§ããªã㯠ã¯ã©ã¹ãå®çŸ©ããŸãã ãã ãã BaseClass 㯠CLS ã«æºæ ããŠããªããããã³ã³ãã€ã©ã¯èŠåãåºåããŸãã
using System;
[assembly:CLSCompliant(true)]
[CLSCompliant(false)] public class BaseClass
{}
public class BaseCollection<T> where T : BaseClass
{}
// Attempting to compile the example displays the following output:
// warning CS3024: Constraint type 'BaseClass' is not CLS-compliant
<Assembly: CLSCompliant(True)>
<CLSCompliant(False)> Public Class BaseClass
End Class
Public Class BaseCollection(Of T As BaseClass)
End Class
' Attempting to compile the example displays the following output:
' warning BC40040: Generic parameter constraint type 'BaseClass' is not
' CLS-compliant.
'
' Public Class BaseCollection(Of T As BaseClass)
' ~~~~~~~~~
ãžã§ããªãã¯åããžã§ããªãã¯åºæ¬åããæŽŸçããŠããå Žåã¯ãåºæ¬åã®å¶çŽãæºããããããã«ãå¶çŽãå宣èšããå¿
èŠããããŸãã æ¬¡ã®äŸã§ã¯ãä»»æã®æ°å€åã衚ãããšãã§ãã Number<T> ãå®çŸ©ããŸãã ãŸããæµ®åå°æ°ç¹å€ã衚ã FloatingPoint<T> ã¯ã©ã¹ãå®çŸ©ããŸãã ãã ãããœãŒã¹ ã³ãŒãã¯ãNumber<T>ã«FloatingPoint<T> (T ã¯å€åã§ããå¿
èŠããããŸã) ã«å¶çŽãé©çšããªããããã³ã³ãã€ã«ã«å€±æããŸãã
using System;
[assembly:CLSCompliant(true)]
public class Number<T> where T : struct
{
// use Double as the underlying type, since its range is a superset of
// the ranges of all numeric types except BigInteger.
protected double number;
public Number(T value)
{
try {
this.number = Convert.ToDouble(value);
}
catch (OverflowException e) {
throw new ArgumentException("value is too large.", e);
}
catch (InvalidCastException e) {
throw new ArgumentException("The value parameter is not numeric.", e);
}
}
public T Add(T value)
{
return (T) Convert.ChangeType(number + Convert.ToDouble(value), typeof(T));
}
public T Subtract(T value)
{
return (T) Convert.ChangeType(number - Convert.ToDouble(value), typeof(T));
}
}
public class FloatingPoint<T> : Number<T>
{
public FloatingPoint(T number) : base(number)
{
if (typeof(float) == number.GetType() ||
typeof(double) == number.GetType() ||
typeof(decimal) == number.GetType())
this.number = Convert.ToDouble(number);
else
throw new ArgumentException("The number parameter is not a floating-point number.");
}
}
// The attempt to compile the example displays the following output:
// error CS0453: The type 'T' must be a non-nullable value type in
// order to use it as parameter 'T' in the generic type or method 'Number<T>'
<Assembly: CLSCompliant(True)>
Public Class Number(Of T As Structure)
' Use Double as the underlying type, since its range is a superset of
' the ranges of all numeric types except BigInteger.
Protected number As Double
Public Sub New(value As T)
Try
Me.number = Convert.ToDouble(value)
Catch e As OverflowException
Throw New ArgumentException("value is too large.", e)
Catch e As InvalidCastException
Throw New ArgumentException("The value parameter is not numeric.", e)
End Try
End Sub
Public Function Add(value As T) As T
Return CType(Convert.ChangeType(number + Convert.ToDouble(value), GetType(T)), T)
End Function
Public Function Subtract(value As T) As T
Return CType(Convert.ChangeType(number - Convert.ToDouble(value), GetType(T)), T)
End Function
End Class
Public Class FloatingPoint(Of T) : Inherits Number(Of T)
Public Sub New(number As T)
MyBase.New(number)
If TypeOf number Is Single Or
TypeOf number Is Double Or
TypeOf number Is Decimal Then
Me.number = Convert.ToDouble(number)
Else
throw new ArgumentException("The number parameter is not a floating-point number.")
End If
End Sub
End Class
' The attempt to compile the example displays the following output:
' error BC32105: Type argument 'T' does not satisfy the 'Structure'
' constraint for type parameter 'T'.
'
' Public Class FloatingPoint(Of T) : Inherits Number(Of T)
' ~
ãã®äŸã§ã¯ãå¶çŽã FloatingPoint<T> ã¯ã©ã¹ã«è¿œå ããããšãæ£åžžã«ã³ã³ãã€ã«ãããŸãã
using System;
[assembly:CLSCompliant(true)]
public class Number<T> where T : struct
{
// use Double as the underlying type, since its range is a superset of
// the ranges of all numeric types except BigInteger.
protected double number;
public Number(T value)
{
try {
this.number = Convert.ToDouble(value);
}
catch (OverflowException e) {
throw new ArgumentException("value is too large.", e);
}
catch (InvalidCastException e) {
throw new ArgumentException("The value parameter is not numeric.", e);
}
}
public T Add(T value)
{
return (T) Convert.ChangeType(number + Convert.ToDouble(value), typeof(T));
}
public T Subtract(T value)
{
return (T) Convert.ChangeType(number - Convert.ToDouble(value), typeof(T));
}
}
public class FloatingPoint<T> : Number<T> where T : struct
{
public FloatingPoint(T number) : base(number)
{
if (typeof(float) == number.GetType() ||
typeof(double) == number.GetType() ||
typeof(decimal) == number.GetType())
this.number = Convert.ToDouble(number);
else
throw new ArgumentException("The number parameter is not a floating-point number.");
}
}
<Assembly: CLSCompliant(True)>
Public Class Number(Of T As Structure)
' Use Double as the underlying type, since its range is a superset of
' the ranges of all numeric types except BigInteger.
Protected number As Double
Public Sub New(value As T)
Try
Me.number = Convert.ToDouble(value)
Catch e As OverflowException
Throw New ArgumentException("value is too large.", e)
Catch e As InvalidCastException
Throw New ArgumentException("The value parameter is not numeric.", e)
End Try
End Sub
Public Function Add(value As T) As T
Return CType(Convert.ChangeType(number + Convert.ToDouble(value), GetType(T)), T)
End Function
Public Function Subtract(value As T) As T
Return CType(Convert.ChangeType(number - Convert.ToDouble(value), GetType(T)), T)
End Function
End Class
Public Class FloatingPoint(Of T As Structure) : Inherits Number(Of T)
Public Sub New(number As T)
MyBase.New(number)
If TypeOf number Is Single Or
TypeOf number Is Double Or
TypeOf number Is Decimal Then
Me.number = Convert.ToDouble(number)
Else
throw new ArgumentException("The number parameter is not a floating-point number.")
End If
End Sub
End Class
å ±éèšèªä»æ§ã§ã¯ãå ¥ãåã«ãªã£ãåãšãããã¯ã ã¡ã³ããŒã«å¯ŸããŠåŸæ¥ã®ã€ã³ã¹ã¿ã³ã¹åããšã®ã¢ãã«ãé©çšãããŸãã ãªãŒãã³ ãžã§ããªãã¯åã¯ãå ¥ãåã«ãªã£ãä¿è·ããããžã§ããªãã¯åã®ç¹å®ã®ã€ã³ã¹ã¿ã³ã¹åãå«ãã·ã°ããã£ãæã€ãã£ãŒã«ããŸãã¯ã¡ã³ããŒãå ¬éã§ããŸããã ãžã§ããªãã¯åºåºã¯ã©ã¹ãŸãã¯ã€ã³ã¿ãŒãã§ã€ã¹ã®ç¹å®ã®ã€ã³ã¹ã¿ã³ã¹åãæ¡åŒµããéãžã§ããªãã¯åã¯ãå ¥ãåã«ãªã£ãä¿è·ããããžã§ããªãã¯åã®å¥ã®ã€ã³ã¹ã¿ã³ã¹åãå«ãã·ã°ããã£ãæã€ãã£ãŒã«ããŸãã¯ã¡ã³ããŒãå ¬éã§ããŸããã
次ã®äŸã§ã¯ããžã§ããªãã¯åã C1<T> (Visual Basic ã§ã¯ C1(Of T) )ãä¿è·ãããã¯ã©ã¹ ã C1<T>.N (ãŸã㯠Visual Basic ã§ã¯ C1(Of T).N ) ãå®çŸ©ããŸãã
C1<T> ã«ã¯ã M1 ãš M2ã® 2 ã€ã®ã¡ãœããããããŸãã ãã ããM1㯠C1C1<int>.NTC1(Of Integer).N (ãŸã㯠<) ãã> (ãŸãã¯C1(Of T)) ãªããžã§ã¯ããè¿ãããšãããããCLS ã«æºæ ããŠããŸããã 2 çªç®ã®ã¯ã©ã¹ ( C2) ã¯ã C1<long> (ãŸã㯠C1(Of Long)) ããæŽŸçããŸãã
M3ãšM4ã® 2 ã€ã®ã¡ãœããããããŸãã
M3㯠CLS ã«æºæ ããŠããŸãããããã¯ãC1<int>.Nã®ãµãã¯ã©ã¹ããC1(Of Integer).N (ãŸãã¯C1<long>) ãªããžã§ã¯ããè¿ãããšããããã§ãã èšèªã³ã³ãã€ã©ã¯ãããã«å¶éãå³ããå ŽåããããŸãã ãã®äŸã§ã¯ãvisual Basic ã¯ã M4ãã³ã³ãã€ã«ããããšãããšãã«ãšã©ãŒã衚瀺ããŸãã
using System;
[assembly:CLSCompliant(true)]
public class C1<T>
{
protected class N { }
protected void M1(C1<int>.N n) { } // Not CLS-compliant - C1<int>.N not
// accessible from within C1<T> in all
// languages
protected void M2(C1<T>.N n) { } // CLS-compliant â C1<T>.N accessible
// inside C1<T>
}
public class C2 : C1<long>
{
protected void M3(C1<int>.N n) { } // Not CLS-compliant â C1<int>.N is not
// accessible in C2 (extends C1<long>)
protected void M4(C1<long>.N n) { } // CLS-compliant, C1<long>.N is
// accessible in C2 (extends C1<long>)
}
// Attempting to compile the example displays output like the following:
// Generics4.cs(9,22): warning CS3001: Argument type 'C1<int>.N' is not CLS-compliant
// Generics4.cs(18,22): warning CS3001: Argument type 'C1<int>.N' is not CLS-compliant
<Assembly: CLSCompliant(True)>
Public Class C1(Of T)
Protected Class N
End Class
Protected Sub M1(n As C1(Of Integer).N) ' Not CLS-compliant - C1<int>.N not
' accessible from within C1(Of T) in all
End Sub ' languages
Protected Sub M2(n As C1(Of T).N) ' CLS-compliant â C1(Of T).N accessible
End Sub ' inside C1(Of T)
End Class
Public Class C2 : Inherits C1(Of Long)
Protected Sub M3(n As C1(Of Integer).N) ' Not CLS-compliant â C1(Of Integer).N is not
End Sub ' accessible in C2 (extends C1(Of Long))
Protected Sub M4(n As C1(Of Long).N)
End Sub
End Class
' Attempting to compile the example displays output like the following:
' error BC30508: 'n' cannot expose type 'C1(Of Integer).N' in namespace
' '<Default>' through class 'C1'.
'
' Protected Sub M1(n As C1(Of Integer).N) ' Not CLS-compliant - C1<int>.N not
' ~~~~~~~~~~~~~~~~
' error BC30389: 'C1(Of T).N' is not accessible in this context because
' it is 'Protected'.
'
' Protected Sub M3(n As C1(Of Integer).N) ' Not CLS-compliant - C1(Of Integer).N is not
'
' ~~~~~~~~~~~~~~~~
'
' error BC30389: 'C1(Of T).N' is not accessible in this context because it is 'Protected'.
'
' Protected Sub M4(n As C1(Of Long).N)
' ~~~~~~~~~~~~~
ã³ã³ã¹ãã©ã¯ã¿ãŒ
CLS æºæ ã®ã¯ã©ã¹ãšæ§é äœã®ã³ã³ã¹ãã©ã¯ã¿ãŒã¯ã次ã®èŠåã«åŸãå¿ èŠããããŸãã
掟çã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãŒã¯ãç¶æ¿ãããã€ã³ã¹ã¿ã³ã¹ ããŒã¿ã«ã¢ã¯ã»ã¹ããåã«ããã®åºåºã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºãå¿ èŠããããŸãã ãã®èŠä»¶ã¯ãåºåºã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãŒã掟çã¯ã©ã¹ã«ãã£ãŠç¶æ¿ãããªãããã§ãã ãã®èŠåã¯ãçŽæ¥ç¶æ¿ããµããŒãããªãæ§é äœã«ã¯é©çšãããŸããã
éåžžãæ¬¡ã®äŸã«ç€ºãããã«ãã³ã³ãã€ã©ã¯ CLS æºæ ãšã¯ç¡é¢ä¿ã«ãã®èŠåãé©çšããŸãã
Doctorã¯ã©ã¹ããæŽŸçããPersonã¯ã©ã¹ãäœæãããŸãããDoctorã¯ã©ã¹ã¯ãç¶æ¿ãããã€ã³ã¹ã¿ã³ã¹ ãã£ãŒã«ããåæåããããã«Personã¯ã©ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºããŸãããusing System; [assembly: CLSCompliant(true)] public class Person { private string fName, lName, _id; public Person(string firstName, string lastName, string id) { if (String.IsNullOrEmpty(firstName + lastName)) throw new ArgumentNullException("Either a first name or a last name must be provided."); fName = firstName; lName = lastName; _id = id; } public string FirstName { get { return fName; } } public string LastName { get { return lName; } } public string Id { get { return _id; } } public override string ToString() { return String.Format("{0}{1}{2}", fName, String.IsNullOrEmpty(fName) ? "" : " ", lName); } } public class Doctor : Person { public Doctor(string firstName, string lastName, string id) { } public override string ToString() { return "Dr. " + base.ToString(); } } // Attempting to compile the example displays output like the following: // ctor1.cs(45,11): error CS1729: 'Person' does not contain a constructor that takes 0 // arguments // ctor1.cs(10,11): (Location of symbol related to previous error)<Assembly: CLSCompliant(True)> Public Class Person Private fName, lName, _id As String Public Sub New(firstName As String, lastName As String, id As String) If String.IsNullOrEmpty(firstName + lastName) Then Throw New ArgumentNullException("Either a first name or a last name must be provided.") End If fName = firstName lName = lastName _id = id End Sub Public ReadOnly Property FirstName As String Get Return fName End Get End Property Public ReadOnly Property LastName As String Get Return lName End Get End Property Public ReadOnly Property Id As String Get Return _id End Get End Property Public Overrides Function ToString() As String Return String.Format("{0}{1}{2}", fName, If(String.IsNullOrEmpty(fName), "", " "), lName) End Function End Class Public Class Doctor : Inherits Person Public Sub New(firstName As String, lastName As String, id As String) End Sub Public Overrides Function ToString() As String Return "Dr. " + MyBase.ToString() End Function End Class ' Attempting to compile the example displays output like the following: ' Ctor1.vb(46) : error BC30148: First statement of this 'Sub New' must be a call ' to 'MyBase.New' or 'MyClass.New' because base class 'Person' of 'Doctor' does ' not have an accessible 'Sub New' that can be called with no arguments. ' ' Public Sub New() ' ~~~ãªããžã§ã¯ã ã³ã³ã¹ãã©ã¯ã¿ãŒã¯ããªããžã§ã¯ããäœæãã以å€ã¯åŒã³åºããŸããã ããã«ããªããžã§ã¯ãã 2 ååæåããããšã¯ã§ããŸããã ããšãã°ãããã¯ã Object.MemberwiseClone ã¡ãœãããšéã·ãªã¢ã«åã¡ãœãããã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºããŠã¯ãªããªãããšãæå³ããŸãã
ããããã£
CLS æºæ åã®ããããã£ã¯ã次ã®èŠåã«åŸãå¿ èŠããããŸãã
ããããã£ã«ã¯ setterãgetterããŸãã¯ãã®äž¡æ¹ãå¿ ãå¿ èŠã§ãã ã¢ã»ã³ããªã§ã¯ããããã¯ç¹æ®ãªã¡ãœãããšããŠå®è£ ãããŸããããã¯ãã¢ã»ã³ããªã®ã¡ã¿ããŒã¿ã§
get_ãšããŠããŒã¯ãããåå¥ã®ã¡ãœãã (getter ã¯set_ãsetter ã¯SpecialName) ãšããŠè¡šç€ºãããããšãæå³ããŸãã C# ããã³ Visual Basic ã³ã³ãã€ã©ã§ã¯ã CLSCompliantAttribute 屿§ãé©çšããå¿ èŠãªãããã®èŠåãèªåçã«é©çšãããŸããããããã£ã®åã¯ããããã㣠getter ã®æ»ãå€ã®åã§ãããã»ãã¿ãŒã®æåŸã®åŒæ°ã§ãã ãããã®å㯠CLS ã«æºæ ããŠããå¿ èŠããããåŒæ°ãåç §ã«ãã£ãŠããããã£ã«å²ãåœãŠãããšã¯ã§ããŸãã (ã€ãŸãããããŒãžã ãã€ã³ã¿ãŒã«ããããšã¯ã§ããŸãã)ã
ããããã£ã«ã²ãã¿ãŒãšã»ãã¿ãŒã®äž¡æ¹ãããå Žåã¯ãäž¡æ¹ãšãä»®æ³ã€ã³ã¹ã¿ã³ã¹ãéçã€ã³ã¹ã¿ã³ã¹ããŸãã¯äž¡æ¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ããå¿ èŠããããŸãã C# ã³ã³ãã€ã©ãš Visual Basic ã³ã³ãã€ã©ã¯ãããããã£å®çŸ©æ§æã䜿çšããŠãã®èŠåãèªåçã«é©çšããŸãã
ã€ãã³ã
ã€ãã³ãã¯ããã®ååãšãã®åã«ãã£ãŠå®çŸ©ãããŸãã ã€ãã³ãã¿ã€ãã¯ãã€ãã³ãã瀺ãããã«äœ¿çšãããããªã²ãŒãã§ãã ããšãã°ã AppDomain.AssemblyResolve ã€ãã³ãã®çš®é¡ã¯ ResolveEventHandler ã§ãã ã€ãã³ãèªäœã«å ããŠãã€ãã³ãåã«åºã¥ãååãæã€ 3 ã€ã®ã¡ãœããã«ãã£ãŠã€ãã³ãã®å®è£
ãæäŸãããã¢ã»ã³ããªã®ã¡ã¿ããŒã¿ã« SpecialName ãšããŠããŒã¯ãããŸãã
add_EventName ãšããååã®ã€ãã³ã ãã³ãã©ãŒã远å ããããã®ã¡ãœããã ããšãã°ã AppDomain.AssemblyResolve ã€ãã³ãã®ã€ãã³ã ãµãã¹ã¯ãªãã·ã§ã³ ã¡ãœããã«ã¯ãadd_AssemblyResolveãšããååãä»ããããŸããremove_EventName ãšããååã®ã€ãã³ã ãã³ãã©ãŒãåé€ããã¡ãœããã ããšãã°ã AppDomain.AssemblyResolve ã€ãã³ãã®åé€ã¡ãœããã®ååã¯remove_AssemblyResolveã§ããã€ãã³ããçºçããããšã瀺ãã¡ãœãã ãååã¯
raise_EventName ã§ãã
泚
å ±éèšèªä»æ§ã®ã€ãã³ãã«é¢ããèŠåã®ã»ãšãã©ã¯ãèšèªã³ã³ãã€ã©ã«ãã£ãŠå®è£ ãããã³ã³ããŒãã³ãéçºè ã«ã¯ééçã§ãã
ã€ãã³ãã远å ãåé€ãããã³çºçãããã¡ãœããã«ã¯ãåãã¢ã¯ã»ã·ããªãã£ãå¿ èŠã§ãã ãããã¯ãã¹ãŠéçãã€ã³ã¹ã¿ã³ã¹ããŸãã¯ä»®æ³ã§ããå¿ èŠããããŸãã ã€ãã³ãã远å ããã³åé€ããããã®ã¡ãœããã«ã¯ãã€ãã³ã ããªã²ãŒãåã®åãæã€ 1 ã€ã®ãã©ã¡ãŒã¿ãŒããããŸãã add ã¡ãœãããš remove ã¡ãœããã®äž¡æ¹ãååšããããäž¡æ¹ãååšããªãå¿ èŠããããŸãã
次ã®äŸã§ã¯ã Temperature ãšããååã® CLS æºæ ã¯ã©ã¹ãå®çŸ©ããŸãããã®ã¯ã©ã¹ã§ã¯ã2 ã€ã®èªã¿åãå€éã®æž©åºŠã®å€åããããå€ãšçãããããããå€ãè¶
ããå Žåã« TemperatureChanged ã€ãã³ããçºçããŸãã
Temperature ã¯ã©ã¹ã¯ãã€ãã³ã ãã³ãã©ãŒãéžæçã«å®è¡ã§ããããã«ãraise_TemperatureChanged ã¡ãœãããæç€ºçã«å®çŸ©ããŸãã
using System;
using System.Collections;
using System.Collections.Generic;
[assembly: CLSCompliant(true)]
public class TemperatureChangedEventArgs : EventArgs
{
private Decimal originalTemp;
private Decimal newTemp;
private DateTimeOffset when;
public TemperatureChangedEventArgs(Decimal original, Decimal @new, DateTimeOffset time)
{
originalTemp = original;
newTemp = @new;
when = time;
}
public Decimal OldTemperature
{
get { return originalTemp; }
}
public Decimal CurrentTemperature
{
get { return newTemp; }
}
public DateTimeOffset Time
{
get { return when; }
}
}
public delegate void TemperatureChanged(Object sender, TemperatureChangedEventArgs e);
public class Temperature
{
private struct TemperatureInfo
{
public Decimal Temperature;
public DateTimeOffset Recorded;
}
public event TemperatureChanged TemperatureChanged;
private Decimal previous;
private Decimal current;
private Decimal tolerance;
private List<TemperatureInfo> tis = new List<TemperatureInfo>();
public Temperature(Decimal temperature, Decimal tolerance)
{
current = temperature;
TemperatureInfo ti = new TemperatureInfo();
ti.Temperature = temperature;
tis.Add(ti);
ti.Recorded = DateTimeOffset.UtcNow;
this.tolerance = tolerance;
}
public Decimal CurrentTemperature
{
get { return current; }
set {
TemperatureInfo ti = new TemperatureInfo();
ti.Temperature = value;
ti.Recorded = DateTimeOffset.UtcNow;
previous = current;
current = value;
if (Math.Abs(current - previous) >= tolerance)
raise_TemperatureChanged(new TemperatureChangedEventArgs(previous, current, ti.Recorded));
}
}
public void raise_TemperatureChanged(TemperatureChangedEventArgs eventArgs)
{
if (TemperatureChanged == null)
return;
foreach (TemperatureChanged d in TemperatureChanged.GetInvocationList()) {
if (d.Method.Name.Contains("Duplicate"))
Console.WriteLine("Duplicate event handler; event handler not executed.");
else
d.Invoke(this, eventArgs);
}
}
}
public class Example
{
public Temperature temp;
public static void Main()
{
Example ex = new Example();
}
public Example()
{
temp = new Temperature(65, 3);
temp.TemperatureChanged += this.TemperatureNotification;
RecordTemperatures();
Example ex = new Example(temp);
ex.RecordTemperatures();
}
public Example(Temperature t)
{
temp = t;
RecordTemperatures();
}
public void RecordTemperatures()
{
temp.TemperatureChanged += this.DuplicateTemperatureNotification;
temp.CurrentTemperature = 66;
temp.CurrentTemperature = 63;
}
internal void TemperatureNotification(Object sender, TemperatureChangedEventArgs e)
{
Console.WriteLine($"Notification 1: The temperature changed from {e.OldTemperature} to {e.CurrentTemperature}");
}
public void DuplicateTemperatureNotification(Object sender, TemperatureChangedEventArgs e)
{
Console.WriteLine($"Notification 2: The temperature changed from {e.OldTemperature} to {e.CurrentTemperature}");
}
}
Imports System.Collections
Imports System.Collections.Generic
<Assembly: CLSCompliant(True)>
Public Class TemperatureChangedEventArgs : Inherits EventArgs
Private originalTemp As Decimal
Private newTemp As Decimal
Private [when] As DateTimeOffset
Public Sub New(original As Decimal, [new] As Decimal, [time] As DateTimeOffset)
originalTemp = original
newTemp = [new]
[when] = [time]
End Sub
Public ReadOnly Property OldTemperature As Decimal
Get
Return originalTemp
End Get
End Property
Public ReadOnly Property CurrentTemperature As Decimal
Get
Return newTemp
End Get
End Property
Public ReadOnly Property [Time] As DateTimeOffset
Get
Return [when]
End Get
End Property
End Class
Public Delegate Sub TemperatureChanged(sender As Object, e As TemperatureChangedEventArgs)
Public Class Temperature
Private Structure TemperatureInfo
Dim Temperature As Decimal
Dim Recorded As DateTimeOffset
End Structure
Public Event TemperatureChanged As TemperatureChanged
Private previous As Decimal
Private current As Decimal
Private tolerance As Decimal
Private tis As New List(Of TemperatureInfo)
Public Sub New(temperature As Decimal, tolerance As Decimal)
current = temperature
Dim ti As New TemperatureInfo()
ti.Temperature = temperature
ti.Recorded = DateTimeOffset.UtcNow
tis.Add(ti)
Me.tolerance = tolerance
End Sub
Public Property CurrentTemperature As Decimal
Get
Return current
End Get
Set
Dim ti As New TemperatureInfo
ti.Temperature = value
ti.Recorded = DateTimeOffset.UtcNow
previous = current
current = value
If Math.Abs(current - previous) >= tolerance Then
raise_TemperatureChanged(New TemperatureChangedEventArgs(previous, current, ti.Recorded))
End If
End Set
End Property
Public Sub raise_TemperatureChanged(eventArgs As TemperatureChangedEventArgs)
If TemperatureChangedEvent Is Nothing Then Exit Sub
Dim ListenerList() As System.Delegate = TemperatureChangedEvent.GetInvocationList()
For Each d As TemperatureChanged In TemperatureChangedEvent.GetInvocationList()
If d.Method.Name.Contains("Duplicate") Then
Console.WriteLine("Duplicate event handler; event handler not executed.")
Else
d.Invoke(Me, eventArgs)
End If
Next
End Sub
End Class
Public Class Example
Public WithEvents temp As Temperature
Public Shared Sub Main()
Dim ex As New Example()
End Sub
Public Sub New()
temp = New Temperature(65, 3)
RecordTemperatures()
Dim ex As New Example(temp)
ex.RecordTemperatures()
End Sub
Public Sub New(t As Temperature)
temp = t
RecordTemperatures()
End Sub
Public Sub RecordTemperatures()
temp.CurrentTemperature = 66
temp.CurrentTemperature = 63
End Sub
Friend Shared Sub TemperatureNotification(sender As Object, e As TemperatureChangedEventArgs) _
Handles temp.TemperatureChanged
Console.WriteLine("Notification 1: The temperature changed from {0} to {1}", e.OldTemperature, e.CurrentTemperature)
End Sub
Friend Shared Sub DuplicateTemperatureNotification(sender As Object, e As TemperatureChangedEventArgs) _
Handles temp.TemperatureChanged
Console.WriteLine("Notification 2: The temperature changed from {0} to {1}", e.OldTemperature, e.CurrentTemperature)
End Sub
End Class
éè² è·
å ±éèšèªä»æ§ã§ã¯ããªãŒããŒããŒããããã¡ã³ããŒã«æ¬¡ã®èŠä»¶ã課ãããŸãã
ã¡ã³ããŒã¯ããã©ã¡ãŒã¿ãŒã®æ°ãšä»»æã®ãã©ã¡ãŒã¿ãŒã®åã«åºã¥ããŠãªãŒããŒããŒãã§ããŸãã åŒã³åºãèŠåãæ»ãå€ã®åãã¡ãœãããŸãã¯ãã®ãã©ã¡ãŒã¿ãŒã«é©çšãããã«ã¹ã¿ã 修食åãããã³ãã©ã¡ãŒã¿ãŒãå€ãŸãã¯åç §ã«ãã£ãŠæž¡ããããã©ããã¯ããªãŒããŒããŒããåºå¥ããéã«ã¯èæ ®ãããŸããã äŸã«ã€ããŠã¯ãã ååä»ãèŠå ãã»ã¯ã·ã§ã³ã®ã¹ã³ãŒãå ã§ååãäžæã«ããå¿ èŠãããèŠä»¶ã®ã³ãŒããåç §ããŠãã ããã
ãªãŒããŒããŒãã§ããã®ã¯ããããã£ãšã¡ãœããã ãã§ãã ãã£ãŒã«ããšã€ãã³ãããªãŒããŒããŒãããããšã¯ã§ããŸããã
ãžã§ããªã㯠ã¡ãœããã¯ããžã§ããªã㯠ãã©ã¡ãŒã¿ãŒã®æ°ã«åºã¥ããŠãªãŒããŒããŒãã§ããŸãã
泚
op_ExplicitæŒç®åãšop_ImplicitæŒç®åã¯ãæ»ãå€ããªãŒããŒããŒã解決ã®ã¡ãœãã ã·ã°ããã£ã®äžéšãšèŠãªãããªãã«ãŒã«ã®äŸå€ã§ãã ããã 2 ã€ã®æŒç®åã¯ããã©ã¡ãŒã¿ãŒãšæ»ãå€ã®äž¡æ¹ã«åºã¥ããŠãªãŒããŒããŒãã§ããŸãã
äŸå€
äŸå€ãªããžã§ã¯ãã¯ã System.Exception ããæŽŸçãããã System.ExceptionããæŽŸçããå¥ã®åããæŽŸçããå¿
èŠããããŸãã æ¬¡ã®äŸã¯ãäŸå€åŠçã« ErrorClass ãšããååã®ã«ã¹ã¿ã ã¯ã©ã¹ã䜿çšããããšãã«çºçããã³ã³ãã€ã© ãšã©ãŒã瀺ããŠããŸãã
using System;
[assembly: CLSCompliant(true)]
public class ErrorClass
{
string msg;
public ErrorClass(string errorMessage)
{
msg = errorMessage;
}
public string Message
{
get { return msg; }
}
}
public static class StringUtilities
{
public static string[] SplitString(this string value, int index)
{
if (index < 0 | index > value.Length) {
ErrorClass badIndex = new ErrorClass("The index is not within the string.");
throw badIndex;
}
string[] retVal = { value.Substring(0, index - 1),
value.Substring(index) };
return retVal;
}
}
// Compilation produces a compiler error like the following:
// Exceptions1.cs(26,16): error CS0155: The type caught or thrown must be derived from
// System.Exception
Imports System.Runtime.CompilerServices
<Assembly: CLSCompliant(True)>
Public Class ErrorClass
Dim msg As String
Public Sub New(errorMessage As String)
msg = errorMessage
End Sub
Public ReadOnly Property Message As String
Get
Return msg
End Get
End Property
End Class
Public Module StringUtilities
<Extension()> Public Function SplitString(value As String, index As Integer) As String()
If index < 0 Or index > value.Length Then
Dim BadIndex As New ErrorClass("The index is not within the string.")
Throw BadIndex
End If
Dim retVal() As String = {value.Substring(0, index - 1),
value.Substring(index)}
Return retVal
End Function
End Module
' Compilation produces a compiler error like the following:
' Exceptions1.vb(27) : error BC30665: 'Throw' operand must derive from 'System.Exception'.
'
' Throw BadIndex
' ~~~~~~~~~~~~~~
ãã®ãšã©ãŒãä¿®æ£ããã«ã¯ã ErrorClass ã¯ã©ã¹ã System.Exceptionããç¶æ¿ããå¿
èŠããããŸãã ãŸããMessage ããããã£ã¯ãªãŒããŒã©ã€ãããå¿
èŠããããŸãã æ¬¡ã®äŸã§ã¯ããããã®ãšã©ãŒãä¿®æ£ããŠãCLS ã«æºæ ãã ErrorClass ã¯ã©ã¹ãå®çŸ©ããŸãã
using System;
[assembly: CLSCompliant(true)]
public class ErrorClass : Exception
{
string msg;
public ErrorClass(string errorMessage)
{
msg = errorMessage;
}
public override string Message
{
get { return msg; }
}
}
public static class StringUtilities
{
public static string[] SplitString(this string value, int index)
{
if (index < 0 | index > value.Length) {
ErrorClass badIndex = new ErrorClass("The index is not within the string.");
throw badIndex;
}
string[] retVal = { value.Substring(0, index - 1),
value.Substring(index) };
return retVal;
}
}
Imports System.Runtime.CompilerServices
<Assembly: CLSCompliant(True)>
Public Class ErrorClass : Inherits Exception
Dim msg As String
Public Sub New(errorMessage As String)
msg = errorMessage
End Sub
Public Overrides ReadOnly Property Message As String
Get
Return msg
End Get
End Property
End Class
Public Module StringUtilities
<Extension()> Public Function SplitString(value As String, index As Integer) As String()
If index < 0 Or index > value.Length Then
Dim BadIndex As New ErrorClass("The index is not within the string.")
Throw BadIndex
End If
Dim retVal() As String = {value.Substring(0, index - 1),
value.Substring(index)}
Return retVal
End Function
End Module
屿§
.NET ã¢ã»ã³ããªã§ã¯ãã«ã¹ã¿ã 屿§ã¯ãã«ã¹ã¿ã 屿§ãæ ŒçŽããã¢ã»ã³ããªãåãã¡ã³ããŒãã¡ãœãã ãã©ã¡ãŒã¿ãŒãªã©ã®ããã°ã©ãã³ã° ãªããžã§ã¯ãã«é¢ããã¡ã¿ããŒã¿ãååŸããããã®æ¡åŒµå¯èœãªã¡ã«ããºã ãæäŸããŸãã ã«ã¹ã¿ã 屿§ã¯ ãSystem.Attribute ããæŽŸçãããã System.AttributeããæŽŸçããåããæŽŸçããå¿
èŠããããŸãã
次ã®äŸã¯ããã®èŠåã«éåããŠããŸãã
NumericAttributeããæŽŸçããªãSystem.Attribute ã¯ã©ã¹ãå®çŸ©ããŸãã ã³ã³ãã€ã© ãšã©ãŒã¯ãã¯ã©ã¹ãå®çŸ©ãããŠããå Žåã§ã¯ãªããCLS ã«æºæ ããŠããªã屿§ãé©çšãããŠããå Žåã«ã®ã¿çºçããŸãã
using System;
[assembly: CLSCompliant(true)]
[AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Struct)]
public class NumericAttribute
{
private bool _isNumeric;
public NumericAttribute(bool isNumeric)
{
_isNumeric = isNumeric;
}
public bool IsNumeric
{
get { return _isNumeric; }
}
}
[Numeric(true)] public struct UDouble
{
double Value;
}
// Compilation produces a compiler error like the following:
// Attribute1.cs(22,2): error CS0616: 'NumericAttribute' is not an attribute class
// Attribute1.cs(7,14): (Location of symbol related to previous error)
<Assembly: CLSCompliant(True)>
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct)> _
Public Class NumericAttribute
Private _isNumeric As Boolean
Public Sub New(isNumeric As Boolean)
_isNumeric = isNumeric
End Sub
Public ReadOnly Property IsNumeric As Boolean
Get
Return _isNumeric
End Get
End Property
End Class
<Numeric(True)> Public Structure UDouble
Dim Value As Double
End Structure
' Compilation produces a compiler error like the following:
' error BC31504: 'NumericAttribute' cannot be used as an attribute because it
' does not inherit from 'System.Attribute'.
'
' <Numeric(True)> Public Structure UDouble
' ~~~~~~~~~~~~~
CLS æºæ 屿§ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãŸãã¯ããããã£ã¯ã次ã®åã®ã¿ãå ¬éã§ããŸãã
次ã®äŸã§ã¯ãDescriptionAttribute ããæŽŸçãã ã¯ã©ã¹ãå®çŸ©ããŸãã ã¯ã©ã¹ ã³ã³ã¹ãã©ã¯ã¿ãŒã«ã¯ Descriptor åã®ãã©ã¡ãŒã¿ãŒããããããã¯ã©ã¹ã¯ CLS ã«æºæ ããŠããŸããã C# ã³ã³ãã€ã©ã¯èŠåãåºåããŸãããæ£åžžã«ã³ã³ãã€ã«ãããŸãããVisual Basic ã³ã³ãã€ã©ã¯èŠåãŸãã¯ãšã©ãŒãåºåããŸããã
using System;
[assembly:CLSCompliantAttribute(true)]
public enum DescriptorType { type, member };
public class Descriptor
{
public DescriptorType Type;
public String Description;
}
[AttributeUsage(AttributeTargets.All)]
public class DescriptionAttribute : Attribute
{
private Descriptor desc;
public DescriptionAttribute(Descriptor d)
{
desc = d;
}
public Descriptor Descriptor
{ get { return desc; } }
}
// Attempting to compile the example displays output like the following:
// warning CS3015: 'DescriptionAttribute' has no accessible
// constructors which use only CLS-compliant types
<Assembly: CLSCompliantAttribute(True)>
Public Enum DescriptorType As Integer
Type = 0
Member = 1
End Enum
Public Class Descriptor
Public Type As DescriptorType
Public Description As String
End Class
<AttributeUsage(AttributeTargets.All)> _
Public Class DescriptionAttribute : Inherits Attribute
Private desc As Descriptor
Public Sub New(d As Descriptor)
desc = d
End Sub
Public ReadOnly Property Descriptor As Descriptor
Get
Return desc
End Get
End Property
End Class
CLSCompliantAttribute 屿§
CLSCompliantAttribute 屿§ã¯ãããã°ã©ã èŠçŽ ãå ±éèšèªä»æ§ã«æºæ ããŠãããã©ããã瀺ãããã«äœ¿çšãããŸãã CLSCompliantAttribute(Boolean) ã³ã³ã¹ãã©ã¯ã¿ãŒã«ã¯ãããã°ã©ã èŠçŽ ã CLS ã«æºæ ããŠãããã©ããã瀺ã 1 ã€ã®å¿ é ãã©ã¡ãŒã¿ãŒ isCompliant ãå«ãŸããŠããŸãã
ã³ã³ãã€ã«æã«ãã³ã³ãã€ã©ã¯ CLS ã«æºæ ããŠãããšæšå®ãããéæºæ ã®èŠçŽ ãæ€åºããèŠåãåºåããŸãã ã³ã³ãã€ã©ã¯ãéæºæ ãšããŠæç€ºçã«å®£èšãããŠããåãŸãã¯ã¡ã³ããŒã«å¯ŸããŠèŠåãåºåããŸããã
ã³ã³ããŒãã³ãéçºè
ã¯ã CLSCompliantAttribute 屿§ã 2 ã€ã®æ¹æ³ã§äœ¿çšã§ããŸãã
CLS ã«æºæ ããŠããã³ã³ããŒãã³ãã«ãã£ãŠå ¬éããããããªã㯠ã€ã³ã¿ãŒãã§ã€ã¹ã®éšåãšãCLS ã«æºæ ããŠããªãéšåãå®çŸ©ããŸãã ãã®å±æ§ã䜿çšããŠç¹å®ã®ããã°ã©ã èŠçŽ ã CLS æºæ ãšããŠããŒã¯ããå Žåããã®äœ¿çšã«ããã.NET ã察象ãšãããã¹ãŠã®èšèªãšããŒã«ãããããã®èŠçŽ ã«ã¢ã¯ã»ã¹ã§ããããã«ãªããŸãã
ã³ã³ããŒãã³ã ã©ã€ãã©ãªã®ãããªã㯠ã€ã³ã¿ãŒãã§ã€ã¹ã CLS ã«æºæ ããŠããããã°ã©ã èŠçŽ ã®ã¿ãå ¬éããããã«ããŸãã èŠçŽ ã CLS ã«æºæ ããŠããªãå Žåãã³ã³ãã€ã©ã¯éåžžèŠåãçºè¡ããŸãã
Warnung
èšèªã³ã³ãã€ã©ã§ã¯ã CLSCompliantAttribute 屿§ã䜿çšãããŠãããã©ããã«é¢ä¿ãªããCLS æºæ ã®èŠåãé©çšãããå ŽåããããŸãã ããšãã°ãã€ã³ã¿ãŒãã§ã€ã¹ã§éçã¡ã³ããŒãå®çŸ©ãããšãCLS èŠåã«éåããŸãã ãã®ç¹ã§ãã€ã³ã¿ãŒãã§ã€ã¹ã§ static (C#) ãŸã㯠Shared (Visual Basic) ã¡ã³ããŒãå®çŸ©ãããšãC# ã³ã³ãã€ã©ãš Visual Basic ã³ã³ãã€ã©ã®äž¡æ¹ã§ãšã©ãŒ ã¡ãã»ãŒãžã衚瀺ãããã¢ããªã®ã³ã³ãã€ã«ã«å€±æããŸãã
CLSCompliantAttribute屿§ã¯ãAttributeUsageAttributeã®å€ãæã€AttributeTargets.All屿§ã§ããŒã¯ãããŸãã ãã®å€ã䜿çšãããšãã¢ã»ã³ããªãã¢ãžã¥ãŒã«ãå (ã¯ã©ã¹ãæ§é äœãåæåãã€ã³ã¿ãŒãã§ã€ã¹ãããªã²ãŒã)ãåã¡ã³ã㌠(ã³ã³ã¹ãã©ã¯ã¿ãŒãã¡ãœãããããããã£ããã£ãŒã«ããã€ãã³ã)ããã©ã¡ãŒã¿ãŒããžã§ããªã㯠ãã©ã¡ãŒã¿ãŒãæ»ãå€ãªã©ãä»»æã®ããã°ã©ã èŠçŽ ã« CLSCompliantAttribute 屿§ãé©çšã§ããŸãã ãã ããå®éã«ã¯ãã¢ã»ã³ããªãåãããã³åã®ã¡ã³ããŒã«ã®ã¿å±æ§ãé©çšããå¿ èŠããããŸãã ãã以å€ã®å Žåãã³ã³ãã€ã©ã¯å±æ§ãç¡èŠããã©ã€ãã©ãªã®ãããªã㯠ã€ã³ã¿ãŒãã§ã€ã¹ã§éæºæ ã®ãã©ã¡ãŒã¿ãŒããžã§ããªã㯠ãã©ã¡ãŒã¿ãŒããŸãã¯æ»ãå€ãæ€åºããããã³ã«ã³ã³ãã€ã©èŠåãçæãç¶ããŸãã
CLSCompliantAttribute屿§ã®å€ã¯ãå å«ããã°ã©ã èŠçŽ ã«ãã£ãŠç¶æ¿ãããŸãã ããšãã°ãã¢ã»ã³ããªã CLS æºæ ãšããŠããŒã¯ãããŠããå Žåããã®åã CLS ã«æºæ ããŠããŸãã åã CLS æºæ ãšããŠããŒã¯ãããŠããå Žåãå ¥ãåã«ãªã£ãåãšã¡ã³ããŒã CLS ã«æºæ ããŸãã
CLSCompliantAttribute屿§ãå
å«ããã°ã©ã èŠçŽ ã«é©çšããããšã§ãç¶æ¿ãããã³ã³ãã©ã€ã¢ã³ã¹ãæç€ºçã«ãªãŒããŒã©ã€ãã§ããŸãã ããšãã°ãCLSCompliantAttribute ã®isCompliantå€ãæã€false屿§ã䜿çšããŠæºæ ã¢ã»ã³ããªã§éæºæ åãå®çŸ©ããisCompliantã®trueå€ãæã€å±æ§ã䜿çšããŠéæºæ ã¢ã»ã³ããªã§æºæ åãå®çŸ©ã§ããŸãã æºæ åã§éæºæ ã¡ã³ããŒãå®çŸ©ããããšãã§ããŸãã ãã ããéæºæ åã¯æºæ ããŠããã¡ã³ããŒãæã€ããšã¯ã§ããŸããããã®ãããisCompliantã®trueå€ãæã€å±æ§ã䜿çšããŠãéæºæ åããã®ç¶æ¿ããªãŒããŒã©ã€ãããããšã¯ã§ããŸããã
ã³ã³ããŒãã³ããéçºãããšãã¯ãã¢ã»ã³ããªããã®åãããã³ãã®ã¡ã³ããŒã CLS ã«æºæ ããŠãããã©ããã瀺ãããã«ãåžžã« CLSCompliantAttribute 屿§ã䜿çšããå¿ èŠããããŸãã
CLS æºæ ã³ã³ããŒãã³ããäœæããã«ã¯:
CLSCompliantAttributeã䜿çšããŠãã¢ã»ã³ããªã CLS æºæ ãšããŠããŒã¯ããŸãã
CLS ã«æºæ ããŠããªãã¢ã»ã³ããªå ã®ãããªãã¯ã«å ¬éãããŠããåãéæºæ ãšããŠããŒã¯ããŸãã
CLS æºæ åã®ãããªãã¯ã«å ¬éãããŠããã¡ã³ããŒãéæºæ ãšããŠããŒã¯ããŸãã
CLS ã«æºæ ããŠããªãã¡ã³ããŒã® CLS æºæ ã®ä»£æ¿ææ®µãæäŸããŸãã
æºæ ããŠããªãåãšã¡ã³ããŒããã¹ãŠæ£åžžã«ããŒã¯ããå Žåãã³ã³ãã€ã©ã¯ã³ã³ãã©ã€ã¢ã³ã¹éåã®èŠåãåºåããªãã§ãã ããã ãã ããCLS ã«æºæ ããŠããªãã¡ã³ããŒãæå®ããCLS æºæ ã®ä»£æ¿ææ®µã補åããã¥ã¡ã³ãã«èšèŒããå¿ èŠããããŸãã
次ã®äŸã§ã¯ã CLSCompliantAttribute 屿§ã䜿çšããŠãCLS æºæ ã®ã¢ã»ã³ããªãšã2 ã€ã®é CLS æºæ ã¡ã³ããŒãæã€å ( CharacterUtilities) ãå®çŸ©ããŸãã äž¡æ¹ã®ã¡ã³ããŒã CLSCompliant(false) 屿§ã§ã¿ã°ä»ããããŠãããããã³ã³ãã€ã©ã¯èŠåãçæããŸããã ãã®ã¯ã©ã¹ã¯ãäž¡æ¹ã®ã¡ãœããã« CLS æºæ ã®ä»£æ¿ææ®µãæäŸããŸãã éåžžã¯ãCLS ã«æºæ ããä»£æ¿ææ®µãæäŸããããã«ã ToUTF16 ã¡ãœããã« 2 ã€ã®ãªãŒããŒããŒãã远å ããã ãã§ãã ãã ããã¡ãœããã¯æ»ãå€ã«åºã¥ããŠãªãŒããŒããŒãã§ããªããããCLS æºæ ã¡ãœããã®ååã¯éæºæ ã¡ãœããã®ååãšã¯ç°ãªããŸãã
using System;
using System.Text;
[assembly:CLSCompliant(true)]
public class CharacterUtilities
{
[CLSCompliant(false)] public static ushort ToUTF16(String s)
{
s = s.Normalize(NormalizationForm.FormC);
return Convert.ToUInt16(s[0]);
}
[CLSCompliant(false)] public static ushort ToUTF16(Char ch)
{
return Convert.ToUInt16(ch);
}
// CLS-compliant alternative for ToUTF16(String).
public static int ToUTF16CodeUnit(String s)
{
s = s.Normalize(NormalizationForm.FormC);
return (int) Convert.ToUInt16(s[0]);
}
// CLS-compliant alternative for ToUTF16(Char).
public static int ToUTF16CodeUnit(Char ch)
{
return Convert.ToInt32(ch);
}
public bool HasMultipleRepresentations(String s)
{
String s1 = s.Normalize(NormalizationForm.FormC);
return s.Equals(s1);
}
public int GetUnicodeCodePoint(Char ch)
{
if (Char.IsSurrogate(ch))
throw new ArgumentException("ch cannot be a high or low surrogate.");
return Char.ConvertToUtf32(ch.ToString(), 0);
}
public int GetUnicodeCodePoint(Char[] chars)
{
if (chars.Length > 2)
throw new ArgumentException("The array has too many characters.");
if (chars.Length == 2) {
if (!Char.IsSurrogatePair(chars[0], chars[1]))
throw new ArgumentException("The array must contain a low and a high surrogate.");
else
return Char.ConvertToUtf32(chars[0], chars[1]);
}
else {
return Char.ConvertToUtf32(chars.ToString(), 0);
}
}
}
Imports System.Text
<Assembly: CLSCompliant(True)>
Public Class CharacterUtilities
<CLSCompliant(False)> Public Shared Function ToUTF16(s As String) As UShort
s = s.Normalize(NormalizationForm.FormC)
Return Convert.ToUInt16(s(0))
End Function
<CLSCompliant(False)> Public Shared Function ToUTF16(ch As Char) As UShort
Return Convert.ToUInt16(ch)
End Function
' CLS-compliant alternative for ToUTF16(String).
Public Shared Function ToUTF16CodeUnit(s As String) As Integer
s = s.Normalize(NormalizationForm.FormC)
Return CInt(Convert.ToInt16(s(0)))
End Function
' CLS-compliant alternative for ToUTF16(Char).
Public Shared Function ToUTF16CodeUnit(ch As Char) As Integer
Return Convert.ToInt32(ch)
End Function
Public Function HasMultipleRepresentations(s As String) As Boolean
Dim s1 As String = s.Normalize(NormalizationForm.FormC)
Return s.Equals(s1)
End Function
Public Function GetUnicodeCodePoint(ch As Char) As Integer
If Char.IsSurrogate(ch) Then
Throw New ArgumentException("ch cannot be a high or low surrogate.")
End If
Return Char.ConvertToUtf32(ch.ToString(), 0)
End Function
Public Function GetUnicodeCodePoint(chars() As Char) As Integer
If chars.Length > 2 Then
Throw New ArgumentException("The array has too many characters.")
End If
If chars.Length = 2 Then
If Not Char.IsSurrogatePair(chars(0), chars(1)) Then
Throw New ArgumentException("The array must contain a low and a high surrogate.")
Else
Return Char.ConvertToUtf32(chars(0), chars(1))
End If
Else
Return Char.ConvertToUtf32(chars.ToString(), 0)
End If
End Function
End Class
ã©ã€ãã©ãªã§ã¯ãªãã¢ããªãéçºããŠããå Žå (ã€ãŸããä»ã®ã¢ããªéçºè ã䜿çšã§ããåãã¡ã³ããŒãå ¬éããŠããªãå Žå)ãã¢ããªã䜿çšããããã°ã©ã èŠçŽ ã® CLS ã³ã³ãã©ã€ã¢ã³ã¹ã¯ãèšèªã§ãµããŒããããŠããªãå Žåã«ã®ã¿éèŠã§ãã ãã®å ŽåãCLS ã«æºæ ããŠããªãèŠçŽ ã䜿çšããããšãããšãèšèªã³ã³ãã€ã©ã§ãšã©ãŒãçæãããŸãã
èšèªéã®çžäºéçšæ§
èšèªã®ç¬ç«ã«ã¯ãããã€ãã®æå³ããããŸãã 1 ã€ã®æå³ã¯ãããèšèªã§èšè¿°ãããåããå¥ã®èšèªã§èšè¿°ãããã¢ããªããã·ãŒã ã¬ã¹ã«äœ¿çšããããšã§ãã ãã®èšäºã®çŠç¹ã§ãã 2 ã€ç®ã®æå³ã¯ãè€æ°ã®èšèªã§èšè¿°ãããã³ãŒãã 1 ã€ã® .NET ã¢ã»ã³ããªã«çµåããããšã§ãã
次ã®äŸã¯ã NumericLib ãš StringLibã® 2 ã€ã®ã¯ã©ã¹ãå«ã Utilities.dll ãšããååã®ã¯ã©ã¹ ã©ã€ãã©ãªãäœæããããšã§ãèšèªéã®çžäºéçšæ§ã瀺ããŠããŸãã
NumericLib ã¯ã©ã¹ã¯ C# ã§èšè¿°ãããStringLib ã¯ã©ã¹ã¯ Visual Basic ã§èšè¿°ãããŸãã
StringUtil.vbã®ãœãŒã¹ ã³ãŒããæ¬¡ã«ç€ºããŸãããã®ã³ãŒãã«ã¯ãToTitleCase ã¯ã©ã¹ã« 1 ã€ã®ã¡ã³ã㌠(StringLib) ãå«ãŸããŠããŸãã
Imports System.Collections.Generic
Imports System.Runtime.CompilerServices
Public Module StringLib
Private exclusions As List(Of String)
Sub New()
Dim words() As String = {"a", "an", "and", "of", "the"}
exclusions = New List(Of String)
exclusions.AddRange(words)
End Sub
<Extension()> _
Public Function ToTitleCase(title As String) As String
Dim words() As String = title.Split()
Dim result As String = String.Empty
For ctr As Integer = 0 To words.Length - 1
Dim word As String = words(ctr)
If ctr = 0 OrElse Not exclusions.Contains(word.ToLower()) Then
result += word.Substring(0, 1).ToUpper() + _
word.Substring(1).ToLower()
Else
result += word.ToLower()
End If
If ctr <= words.Length - 1 Then
result += " "
End If
Next
Return result
End Function
End Module
NumericLibãšIsEvenã® 2 ã€ã®ã¡ã³ããŒãæã€NearZero ã¯ã©ã¹ãå®çŸ©ãããNumberUtil.csã®ãœãŒã¹ ã³ãŒããæ¬¡ã«ç€ºããŸãã
using System;
public static class NumericLib
{
public static bool IsEven(this IConvertible number)
{
if (number is Byte ||
number is SByte ||
number is Int16 ||
number is UInt16 ||
number is Int32 ||
number is UInt32 ||
number is Int64)
return Convert.ToInt64(number) % 2 == 0;
else if (number is UInt64)
return ((ulong) number) % 2 == 0;
else
throw new NotSupportedException("IsEven called for a non-integer value.");
}
public static bool NearZero(double number)
{
return Math.Abs(number) < .00001;
}
}
2 ã€ã®ã¯ã©ã¹ã 1 ã€ã®ã¢ã»ã³ããªã«ããã±ãŒãžåããã«ã¯ãããããã¢ãžã¥ãŒã«ã«ã³ã³ãã€ã«ããå¿ èŠããããŸãã Visual Basic ãœãŒã¹ ã³ãŒã ãã¡ã€ã«ãã¢ãžã¥ãŒã«ã«ã³ã³ãã€ã«ããã«ã¯ã次ã®ã³ãã³ãã䜿çšããŸãã
vbc /t:module StringUtil.vb
Visual Basic ã³ã³ãã€ã©ã®ã³ãã³ã ã©ã€ã³æ§æã®è©³çްã«ã€ããŠã¯ãã ã³ãã³ã ã©ã€ã³ããã®ãã«ãããåç §ããŠãã ããã
C# ãœãŒã¹ ã³ãŒã ãã¡ã€ã«ãã¢ãžã¥ãŒã«ã«ã³ã³ãã€ã«ããã«ã¯ã次ã®ã³ãã³ãã䜿çšããŸãã
csc /t:module NumberUtil.cs
次ã«ã ãªã³ã«ãŒ ãªãã·ã§ã³ ã䜿çšããŠã2 ã€ã®ã¢ãžã¥ãŒã«ãã¢ã»ã³ããªã«ã³ã³ãã€ã«ããŸãã
link numberutil.netmodule stringutil.netmodule /out:UtilityLib.dll /dll
次ã®äŸã§ã¯ã NumericLib.NearZero ã¡ãœãããš StringLib.ToTitleCase ã¡ãœãããåŒã³åºããŸãã Visual Basic ã³ãŒããš C# ã³ãŒãã®äž¡æ¹ã§ãäž¡æ¹ã®ã¯ã©ã¹ã®ã¡ãœããã«ã¢ã¯ã»ã¹ã§ããŸãã
using System;
public class Example
{
public static void Main()
{
Double dbl = 0.0 - Double.Epsilon;
Console.WriteLine(NumericLib.NearZero(dbl));
string s = "war and peace";
Console.WriteLine(s.ToTitleCase());
}
}
// The example displays the following output:
// True
// War and Peace
Module Example
Public Sub Main()
Dim dbl As Double = 0.0 - Double.Epsilon
Console.WriteLine(NumericLib.NearZero(dbl))
Dim s As String = "war and peace"
Console.WriteLine(s.ToTitleCase())
End Sub
End Module
' The example displays the following output:
' True
' War and Peace
Visual Basic ã³ãŒããã³ã³ãã€ã«ããã«ã¯ã次ã®ã³ãã³ãã䜿çšããŸãã
vbc example.vb /r:UtilityLib.dll
C# ã§ã³ã³ãã€ã«ããã«ã¯ãã³ã³ãã€ã©ã®ååã vbc ãã csc ã«å€æŽãããã¡ã€ã«æ¡åŒµåã .vb ãã .cs ã«å€æŽããŸãã
csc example.cs /r:UtilityLib.dll