Skip to content

Commit

Permalink
required修飾子のサンプルを追加 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuna-can-se authored May 5, 2024
1 parent f09e68b commit b165b4f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
19 changes: 19 additions & 0 deletions text/10/ConstructorSample/Book1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace ConstructorSample;

public class Book1
{
public Book1(string title, string author)
{
this.Title = title;
this.Author = author;
}

public string Title { get; set; }

public string Author { get; set; }

public override string ToString()
{
return $"Title:{this.Title}, Author:{this.Author}";
}
}
13 changes: 13 additions & 0 deletions text/10/ConstructorSample/Book2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace ConstructorSample;

public class Book2
{
public required string Title { get; set; }

public required string Author { get; set; }

public override string ToString()
{
return $"Title:{this.Title}, Author:{this.Author}";
}
}
9 changes: 9 additions & 0 deletions text/10/ConstructorSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@

Console.WriteLine(p0_1);
Console.WriteLine(p1_2);

Book1 book1 = new Book1("Visual Studioの本", "たろう");
Book2 book2 = new Book2() { Title = "C#の本", Author = "はなこ" };

// コンパイルエラーになる例
// Book2 book2 = new Book2() { Title = "C#の本" };

Console.WriteLine(book1);
Console.WriteLine(book2);
4 changes: 2 additions & 2 deletions text/TextSample.sln
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncMain", "17\AsyncMain\A
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncMainWithTopLevelStatement", "17\AsyncMainWithTopLevelStatement\AsyncMainWithTopLevelStatement.csproj", "{D32CE6DF-FD50-4161-9419-91E5E811EA27}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwitchExpression", "08\SwitchExpression\SwitchExpression.csproj", "{923ED07E-2BD7-43BD-949D-44C1B6F1E332}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SwitchExpression", "08\SwitchExpression\SwitchExpression.csproj", "{923ED07E-2BD7-43BD-949D-44C1B6F1E332}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrimaryConstructor", "14\PrimaryConstructor\PrimaryConstructor.csproj", "{DC909BD1-3684-4F16-BB2B-6210B8FAEB26}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrimaryConstructor", "14\PrimaryConstructor\PrimaryConstructor.csproj", "{DC909BD1-3684-4F16-BB2B-6210B8FAEB26}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down

0 comments on commit b165b4f

Please sign in to comment.