C# のプログラムは型の集合

➨ 2 ❶㻌 C# 䛾ᇶᮏᵓᩥ
㻌
䕔 㻯㻏㻌 䛾䝥䝻䜾䝷䝮䛿ᆺ䛾㞟ྜ㻌
C#䛷సᡂ䛥䜜䜛䝥䝻䜾䝷䝮䛿䛩䜉䛶ᆺ䛾㞟ྜయ(䝁䝺䜽䝅䝵䞁)䛸䛧䛶సᡂ䛥䜜䜎䛩䚹ฎ⌮䛾䜏䜢༢⊂
䛷グ㏙䛫䛪䛻䚸ఱ䜙䛛䛾ᆺ(䝴䞊䝄䞊ᐃ⩏ᆺ)䛻ྵ䜑䜎䛩䚹
௨ୗ䛾䝁䞊䝗౛䛷䛿䚸MainClass 䛸䛔䛖ྡ๓䛷ᆺ䜢ᐃ⩏䛧䚸䛭䛾୰䛷 Main 䛸䛔䛖ྡ๓䛷ฎ⌮䜢ᐃ⩏
䛧䛶䛔䜎䛩䚹
䖃 䝁䞊䝗౛䠖ᆺ㻌
1. class MainClass
2. {
3.
static void Main()
4.
{
5.
System.Console.WriteLine("Hello C#");
6.
System.Console.WriteLine("Hello Visual Studio");
7.
}
8. }
㻌
㻌
18
➨ 2 ❶㻌 C# 䛾ᇶᮏᵓᩥ
㻌
䕔 䝣䝸䞊䝣䜷䞊䝬䝑䝖㻌
C# 䛾䝋䞊䝇䝁䞊䝗䛾グ㏙ᵝᘧ䛿䚸ẚ㍑ⓗ⮬⏤䛻✵ⓑ䜔ᨵ⾜䜢ᤄධ䛩䜛䛣䛸䛜䛷䛝䜎䛩䚹
䕔 ኱ᩥᏐ䚸ᑠᩥᏐ䜢༊ู㻌
C# 䛷䛿䚸኱ᩥᏐ䞉ᑠᩥᏐ䛿ཝᐦ䛻༊ู䛥䜜䜎䛩(Case-Sensitive)䚹䛯䛸䛘䜀䚸䛂main䛃䛸䛂Main䛃䚸
䛚䜘䜃䛂MAIN䛃䛿䚸䜎䛳䛯䛟ู䛾ྡ๓(㆑ูᏊ)䛸䛧䛶ホ౯䛥䜜䜎䛩䚹
䕔 䝇䝔䞊䝖䝯䞁䝖༊ษ䜚㻌
ᐇ⾜䛥䛫䜛࿨௧䛿䚸䝇䝔䞊䝖䝯䞁䝖䛸䛧䛶グ㏙䛧䚸୍䛴୍䛴䛾䝇䝔䞊䝖䝯䞁䝖䛾༊ษ䜚䜢䛂䠗䛃(䝉䝭䝁䝻䞁)
䛷䛒䜙䜟䛧䜎䛩䚹
䕔 䝯䝋䝑䝗㻌
୍⯡ⓗ䛺䝥䝻䜾䝷䝭䞁䜾ゝㄒ䛸ྠᵝ䛻䚸䛒䜛⛬ᗘ䜎䛸䜎䛳䛯ព࿡䜢䜒䛴୍㐃䛾ฎ⌮䛿䚸䜂䛸䜎䛸䜑䛷
䛒䜙䜟䛩䛣䛸䛜䛷䛝䜎䛩䚹䛭䛾䜎䛸䜎䜚䜢 C# 䛷䛿䛂䝯䝋䝑䝗䛃䛸࿧䜃䜎䛩(௚䛾䝥䝻䜾䝷䝭䞁䜾ゝㄒ䛷
䛂㛵ᩘ䛃䜔䛂䝥䝻䝅䞊䝆䝱䛃䛸࿧䜀䜜䛶䛔䜛䜒䛾䛸ྠᵝ䛷䛩)䚹
䝯䝋䝑䝗䜢⏝䛔䜛䛣䛸䛷䚸ᵓ㐀ⓗ䛻ᢕᥱ䛧䜔䛩䛔䝥䝻䜾䝷䝮䛜సᡂ䛷䛝䜎䛩䛜䚸C# 䛷䛿䝯䝋䝑䝗䛾䜏
䜢༢⊂䛷グ㏙䛩䜛䛣䛸䛜䛷䛝䜎䛫䜣(๓㡫)䚹䛭䛣䛷䚸ᆺ䛾䜂䛸䛴䛷䛒䜛䛂䜽䝷䝇䛃䛾ᵓ㐀ෆ䛷䝯䝋䝑䝗䜢
ᐃ⩏䛧䜎䛩䚹
㻌
㻌
19
➨ 2 ❶㻌 C# 䛾ᇶᮏᵓᩥ
㻌
䕔 䝥䝻䜾䝷䝮䛾㛤ጞⅬ㻌 㻔䜶䞁䝖䝸䝫䜲䞁䝖㻕㻌
䝯䝋䝑䝗䜢ᐃ⩏䛩䜛ሙྜ䚸䛒䜙䛛䛨䜑ព࿡௜䛡䛥䜜䛯ᩥᏐิ(䜻䞊䝽䞊䝗)䜢㝖䛡䜀䚸௵ព䛾ྡ๓䜢䝯
䝋䝑䝗䛻䛴䛡䜛䛣䛸䛜ྍ⬟䛷䛩䚹䛧䛛䛧䚸䛂Main䛃䛸䛔䛖ྡ๓䛾䝯䝋䝑䝗䛿䚸≉ู䛺ព࿡䜢ᣢ䛱䜎䛩䚹
C# 䛾䝥䝻䜾䝷䝮䛿䚸䛂Main䛃䛸䛔䛖ྡ๓䛾䝯䝋䝑䝗䛛䜙ฎ⌮䛜ጞ䜎䜚䜎䛩䚹1 䛴䛾䝥䝻䜾䝷䝮ෆ䛻
Main 䝯䝋䝑䝗䛜䜂䛸䛴䛰䛡ᐃ⩏䛥䜜䛶䛔䜜䜀䚸䛭䜜䛜䝥䝻䜾䝷䝮䛾㛤ጞⅬ(䜶䞁䝖䝸䝫䜲䞁䝖)䛻䛺䜚䜎
䛩䚹䜎䛯䚸Main 䝯䝋䝑䝗䜢」ᩘ䛾䜽䝷䝇䛻ᐃ⩏䛩䜛䛣䛸䛷䚸2 䛴௨ୖ Main 䝯䝋䝑䝗䜢ྵ䜣䛰䝥䝻䜾䝷
䝮䜢సᡂ䛩䜛䛣䛸䜒ྍ⬟䛷䛩䚹䛭䛾ሙྜ䛿䚸䜶䞁䝖䝸䝫䜲䞁䝖䛻䛩䜛 Main 䝯䝋䝑䝗䜢ྵ䜐䜽䝷䝇䜢ᣦᐃ
䛧䜎䛩䚹
ୖ䛾䝁䞊䝗౛䛷䛿䚸Main 䝯䝋䝑䝗䛾୰䛷䚸WriteLine 䝯䝋䝑䝗䛻䜘䜚䛂Hello C#䛃䛚䜘䜃䡞Hello Visual
Studio䡟䛸䛔䛖ᩥᏐิ䜢⾲♧䛧䛶䛔䜎䛩䚹WriteLine 䝯䝋䝑䝗䛿䚸.NET Framework 䜽䝷䝇䝷䜲䝤䝷䝸䛜
ᥦ౪䛩䜛䝯䝋䝑䝗䛾୍䛴䛷䛩䚹.NET Framework 䛜ᥦ౪䛩䜛䝯䝋䝑䝗䜒䚸ఱ䜙䛛䛾ᆺ䛻ྵ䜎䜜䜎䛩䚹
䛂WriteLine䛃䛾๓䛾䛂Console䛃䛜䚸WriteLine 䝯䝋䝑䝗䛜ྵ䜎䜜䜛䜽䝷䝇ྡ䛷䛩䚹䜎䛯䚸䛂Console䛃䛾
๓䛻グ㏙䛥䜜䜛䛂System䛃䛿䚸Console 䜽䝷䝇䛜ྵ䜎䜜䜛ྡ๓✵㛫䛷䛩䚹
㻌
㻌
20
➨ 2 ❶㻌 C# 䛾ᇶᮏᵓᩥ
㻌
ྡ๓✵㛫䛿䚸䜽䝷䝇䛺䛹䛾ᆺ䜢ㄽ⌮ⓗ䛻䜾䝹䞊䝥໬䛩䜛䜒䛾䛷䛩䚹ྠ䛨ྡ๓䛾䜽䝷䝇䛷䛒䛳䛶䜒䚸䛭
䜜䛜ྵ䜎䜜䜛ྡ๓✵㛫䛜␗䛺䜜䜀䚸␗䛺䜛䜽䝷䝇䛸䜏䛺䛥䜜䜛䛯䜑䚸䜽䝷䝇ྡ䛾㔜」䜢㜵䛠䛣䛸䛜䛷
䛝䜎䛩䚹
ྡ๓✵㛫䛿㝵ᒙᵓ㐀䜢ᙧᡂ䛩䜛䛣䛸䛜ྍ⬟䛷䛩䚹.NET Framework 䜽䝷䝇䝷䜲䝤䝷䝸䛷䛿 System
ྡ๓✵㛫䜢㡬Ⅼ䛻」ᩘ䛾ྡ๓✵㛫䛻ᯞศ䛛䜜䛧䛶䚸ከ䛟䛾䜽䝷䝇䛜⟶⌮䛥䜜䛶䛔䜎䛩䚹
≉ᐃ䛾ྡ๓✵㛫䛾䜽䝷䝇䜢฼⏝䛩䜛䛻䛿䚸๓䝨䞊䝆䛾䛂System.Console䛃䛾䜘䛖䛻䚸䛂.䛃(䝗䝑䝖)௜䛝
䛾ྡ๓✵㛫ྡ䛷䜽䝷䝇ྡ䜢ಟ㣭䛩䜛䛛䚸䜎䛯䛿䛂using䛃䜻䞊䝽䞊䝗䛷ྡ๓✵㛫䛾౑⏝䜢ᐉゝ䛧䛶䚸ᬯ
㯲ⓗ䛻䜽䝷䝇ྡ䜢ྡ๓✵㛫䛷ಟ㣭䛧䜎䛩䚹
䕔 䝁䞊䝗౛䠖ྡ๓✵㛫㻌
1. using System;
2.
3. class MainClass
4. {
5.
static void Main()
6.
{
7.
Console.WriteLine("Hello C#");
8.
Console.WriteLine("Hello Visual Studio");
9.
}
10. }
㻌
㻌
21
➨ 2 ❶㻌 C# 䛾ᇶᮏᵓᩥ
㻌
䝋䞊䝇䝁䞊䝗ୖ䛻グ䛥䜜䜛ㄝ᫂᭩䛝䛺䛹௵ព䛾ᩥᏐิ䛿䚸䝁䝯䞁䝖䛸䛧䛶䛒䜙䜟䛧䜎䛩䚹
䛂/*
*/䛃䛿䚸䝁䝯䞁䝖䛾㛤ጞ఩⨨䛸⤊஢఩⨨䜢᫂♧䛧䛯ᙧᘧ䛷䛩䚹」ᩘ⾜䛻䜟䛯䛳䛶䝁䝯䞁䝖䜢᭩䛟䛣
䛸䛜䛷䛝䜎䛩䚹
䛂//䛃䛿䚸༢୍⾜䛾䜏䛷฼⏝䛩䜛䝁䝯䞁䝖䛷䛩䚹//䜢グ㏙䛧䛯఩⨨䛛䜙䚸䛭䛾⾜䛾⤊䜟䜚䜎䛷䛜䝁䝯䞁䝖
䛸䛧䛶ᢅ䜟䜜䜎䛩䚹
䕔 䝁䞊䝗౛䠖䝁䝯䞁䝖㻌
1. using System; // ฼⏝ࡍࡿྡ๓✵㛫ࡢᐉゝ
2. class MainClass
3. {
4.
/*
5.
࢔ࣉࣜࢣ࣮ࢩࣙࣥࡢ㛤ጞⅬ
6.
ᩥᏐิࢆ 2 ⾜ฟຊࡋ࡚⤊஢ࡋࡲࡍ
7.
*/
8.
static void Main()
9.
{
10.
Console.WriteLine("Hello C#"); // ᩥᏐิࢆ⾲♧
11.
Console.WriteLine("Hello Visual Studio");
12.
}
13. }
㻌
㻌
22
➨ 2 ❶㻌 C# 䛾ᇶᮏᵓᩥ
㻌
㻌
㻌
23
➨ 2 ❶㻌 C# 䛾ᇶᮏᵓᩥ
㻌
䕔 ኚᩘᐉゝ䛜ᚲ㡲㻌
C#䛷ኚᩘ䜢฼⏝䛩䜛䛯䜑䛻䛿䚸ᚲ䛪ኚᩘᐉゝ䛜ᚲせ䛻䛺䜚䜎䛩䚹ኚᩘᐉゝ䛾グ㏙ἲ䛸䛧䛶䛿䚸௨
ୗ䛾䜘䛖䛺ᙧᘧ䜢౑⏝䛧䜎䛩䚹
ࢹ࣮ࢱᆺ ኚᩘྡ;
౛䛘䜀䚸int 䛸䛔䛖䝕䞊䝍ᆺ (ᩚᩘ䜢ᢅ䛘䜛䝕䞊䝍ᆺ䛷䛩) 䛷䚸data 䛸䛔䛖ྡ๓䛾ኚᩘ䜢ᐉゝ䛩䜛䛻
䛿䚸ḟ䛾䜘䛖䛻グ㏙䛧䜎䛩䚹
1. int data;
䕔 ᐉゝ᫬䛻ึᮇ໬䛜ྍ⬟㻌
ኚᩘ䛿ᐉゝ䛸ྠ᫬䛻ึᮇ್䜢ᣦᐃ䛩䜛䛣䛸䜒ྍ⬟䛷䛩䚹ึᮇ್䜢୚䛘䜛ሙྜ䛿䚸=(䜲䝁䞊䝹)䜢౑⏝
䛧䛶ึᮇ್䜢୚䛘䜎䛩䚹
1. int data = 100;
䕔 䝇䝁䞊䝥㻌
ኚᩘ䜢ᐉゝ䛩䜛ሙᡤ䛿䚸䜽䝷䝇ෆ䛷䛒䜜䜀䚸䛹䛣䛷ᐉゝ䛧䛶䜒ᩥἲୖ䛿ṇ䛧䛔䜒䛾䛻䛺䜚䜎䛩䚹䛧䛛
䛧䚸ᐉゝ䛩䜛ሙᡤ䛻䜘䛳䛶䚸䛭䛾ኚᩘ䛻䜰䜽䝉䝇䛷䛝䜛䝁䞊䝗䛾⠊ᅖ䛜ኚ䜟䜚䜎䛩䚹䛣䛾䜰䜽䝉䝇ྍ⬟
䛺⠊ᅖ䜢䝇䝁䞊䝥䛸࿧䜃䜎䛩䚹䝯䝋䝑䝗ෆ䛷ᐉゝ䛥䜜䛯ኚᩘ䛿䚸䛭䛾䝯䝋䝑䝗ෆ䛛䜙䛾䜏ኚᩘ䜈䛾䜰䜽
䝉䝇䛜⾜䛘䚸䜽䝷䝇ෆ(䝯䝋䝑䝗䛾እഃ)䛷ᐉゝ䛥䜜䛯ኚᩘ䛿䚸䛭䛾䜽䝷䝇ෆ䛾ྛ䝯䝋䝑䝗䛛䜙䜰䜽䝉䝇䛜
⾜䛘䜎䛩䚹䛣䛾❶䛷䛿䚸䝯䝋䝑䝗ෆ䛷ᐉゝ䛥䜜䜛ኚᩘ䛻䛴䛔䛶ᢅ䛔䜎䛩䚹䛺䛚䚸䜽䝷䝇ෆ䛷ᐉゝ䛥䜜䜛
ኚᩘ䛻䛴䛔䛶䛿 3 ❶䛷ᢅ䛔䜎䛩䚹
㻌
㻌
24
➨ 2 ❶㻌 C# 䛾ᇶᮏᵓᩥ
㻌
䕔 ண⣙ㄒ㻌 䠄䜻䞊䝽䞊䝗䠅㻌 䛸䛧䛶⏝ព䛥䜜䛶䛔䜛䝕䞊䝍ᆺ㻌
C#䛷䛿⤌䜏㎸䜏ᆺ䛿ண⣙ㄒ(䜻䞊䝽䞊䝗)䜢⏝䛔䛶⾲䛩䛣䛸䛜䛷䛝䜎䛩䚹C#䛾⤌䜏㎸䜏ᆺ䛿䚸ᩚᩘ䚸
ᐇᩘ䚸ᩥᏐ䚸ᩥᏐิ䛺䛹䚸୍⯡ⓗ䛺䝕䞊䝍⾲⌧䛻฼⏝䛥䜜䜛䝕䞊䝍ᆺ䛜ᥦ౪䛥䜜䛶䛔䜎䛩䚹
䕔 ᐃᩘ䛾⾲グ㻌
䝁䞊䝗ෆ䛻ᩚᩘ䜢グ㏙䛩䜛㝿䚸ㄒᑿ䛻ఱ䜒䛴䛡䛺䛔䛸䛝䛿䚸䝁䞁䝟䜲䝷䛿 int䚸uint䚸long䚸ulong 䛾
㡰䛻↷䜙䛧ྜ䜟䛫䚸್䜢᱁⣡ྍ⬟䛺኱䛝䛥䛾䝕䞊䝍ᆺ䛸䛧䛶ᢅ䛔䜎䛩䚹䜎䛯䚸䝁䞊䝗ෆ䛻ᑠᩘⅬ䜢ྵ䜐
ᩘ್䜢グ㏙䛩䜛ሙྜ䛿䚸ㄒᑿ䛻ఱ䜒䛴䛡䛺䛔䛸䛝䛿䚸䝁䞁䝟䜲䝷䛿 double 䛸䛧䛶ᢅ䛔䜎䛩䚹䛭䜜௨
እ䛾䝕䞊䝍ᆺ䛸䛧䛶ᢅ䛔䛯䛔䛸䛝䛿䚸௨ୗ䛾䜘䛖䛻ᩘ್䛾ᚋ䛻䝕䞊䝍ᆺ䜢⾲䛩್䜢ᣦᐃ䛧䜎䛩䚹
䝕䞊䝍ᆺ
グ㏙౛
uint
100u䚸100U
ulong
100ul䚸100UL
float
3.14f䚸3.14F
double
3.14d䚸3.14D
decimal
1.05m䚸1.05M
char
'a'䚸'A'
string
"Hello"
䕔 㼛㼎㼖㼑㼏㼠㻌 ᆺ㻌
object ᆺ䛿䚸䛩䜉䛶䛾䝕䞊䝍ᆺ䛾ᇶ♏䛸䛺䜛ỗ⏝ⓗ䛺䝕䞊䝍ᆺ䛷䛩䚹䛩䜉䛶䛾䝕䞊䝍ᆺ䛾್䛜䚸
object ᆺ䛾ኚᩘ䛻௦ධྍ⬟䛷䛩䚹
㻌
25
➨ 2 ❶㻌 C# 䛾ᇶᮏᵓᩥ
䕔 㻯㻏㻌 䛾⤌䜏㎸䜏ᆺ䛿䚸㻚㻺㻱㼀㻌 㻲㼞㼍㼙㼑㼣㼛㼞㼗㻌 䛾㻌 㻯㼀㻿㻌 ᆺ䛾ูྡ㻌
C# 䛷ᥦ౪䛥䜜䛶䛔䜛⤌䜏㎸䜏䛾䝕䞊䝍ᆺ䛿䚸.NET Framework 䛾㻌 CTS (ඹ㏻ᆺ䝅䝇䝔䝮)䛷ᥦ
౪䛥䜜䜛ᵓ㐀య䜔䜽䝷䝇䛾ูྡ(䜶䜲䝸䜰䝇)䛻䛒䛯䜚䜎䛩䚹C# 䛾ᆺ䛸䚸CTS 䛾ᆺ䛿༊ู䛥䜜䜎䛫䜣䚹
䝁䞊䝗䛷䛿䚸C# 䛾ᆺ䜒 CTS 䛾ᆺ䜒䛹䛱䜙䜒฼⏝ྍ⬟䛷䛩䚹
㻌
㻌
26
➨ 2 ❶㻌 C# 䛾ᇶᮏᵓᩥ
㻌
䕔 䝕䞊䝍ᆺ䜢ᣦᐃ䛧䛺䛔ኚᩘᐉゝ㻌
C#䛷䛿 var 䜢฼⏝䛩䜛䛸䚸䝕䞊䝍ᆺ䜢ᣦᐃ䛫䛪䛻ኚᩘ䜢ᐉゝ䛩䜛䛣䛸䛜䛷䛝䜎䛩䚹䝕䞊䝍ᆺ䜢ᣦᐃ
䛫䛪䛻ᐉゝ䛧䛯ኚᩘ䜢䚸ᬯ㯲ⓗ䛻ᆺᣦᐃ䛥䜜䛯ኚᩘ䛸䛔䛔䜎䛩䚹ᬯ㯲ⓗ䛻ᆺᣦᐃ䛥䜜䛯ኚᩘ䛾グ㏙
ἲ䛸䛧䛶䛿䚸௨ୗ䛾䜘䛖䛺ᙧᘧ䜢౑⏝䛧䜎䛩䚹
var ኚᩘྡ = ึᮇ್;
䕔 ึᮇ໬㻌
ᬯ㯲ⓗ䛻ᆺᣦᐃ䛥䜜䛯ኚᩘ䛿䚸䝕䞊䝍ᆺ䜢ึᮇ್䛛䜙᥎ 䛧䜎䛩䚹䛭䛾䛯䜑䚸ᐉゝ᫬䛻ᚲ䛪ึᮇ
್䜢ᣦᐃ䛩䜛ᚲせ䛜䛒䜚䜎䛩䚹ึᮇ್䜢ᣦᐃ䛧䛶䛔䛺䛔ሙྜ䛿䚸䝁䞁䝟䜲䝷䛜䝕䞊䝍ᆺ䜢᥎ 䛷䛝䛺
䛔䛯䜑䚸䝁䞁䝟䜲䝹䜶䝷䞊䛻䛺䜚䜎䛩䚹䜎䛯䚸ึᮇ್䛸䛧䛶 null 䜢౑⏝䛩䜛䛣䛸䜒䛷䛝䜎䛫䜣䚹
䕔 䝁䞊䝗౛䠖ᬯ㯲ⓗ䛻ᆺᣦᐃ䛥䜜䛯ኚᩘ㻌
1. class MainClass
2. {
3.
static void Main()
4.
{
5.
var i = 5;
// int ᆺ
6.
var s1 = "Hello";
// string ᆺ
7.
//var s2;
// ࢥࣥࣃ࢖࢚࣮ࣝࣛ
8.
//var s3 = null;
// ࢥࣥࣃ࢖࢚࣮ࣝࣛ
9.
}
10. }
㻌
㻌
27