Skip to content

Commit ed54a2c

Browse files
committed
add UsingStaticSamples01.cs
1 parent d61d2af commit ed54a2c

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using TryCSharp.Common;
2+
3+
// using static の利用
4+
// これにより、Consoleクラスのstaticメソッドをクラス名修飾なしで利用出来る
5+
// python の from xxx import xxx と同じような感じ。
6+
using static System.Console;
7+
8+
namespace TryCSharp.Samples.CSharp6
9+
{
10+
using static MyStaticClass;
11+
12+
[Sample]
13+
public class UsingStaticSamples01 : IExecutable
14+
{
15+
public void Execute()
16+
{
17+
// Consoleクラスに対して、using static しているので
18+
// メソッド呼び出しにConsoleを付けなくても呼べる。
19+
WriteLine("hello world");
20+
21+
// 当然、自前のクラスに対しても using static 出来る
22+
MyStaticMethod("hello world");
23+
}
24+
}
25+
26+
public static class MyStaticClass
27+
{
28+
public static void MyStaticMethod(string message)
29+
{
30+
Output.WriteLine(message);
31+
}
32+
}
33+
}

TryCSharp.Samples/TryCSharp.Samples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
<Compile Include="Commons\Extensions\StringExtensions.cs" />
147147
<Compile Include="CSharp6\AutoPropSamples01.cs" />
148148
<Compile Include="CSharp6\ExpressionBodySamples01.cs" />
149+
<Compile Include="CSharp6\UsingStaticSamples01.cs" />
149150
<Compile Include="Dummy.cs" />
150151
<Compile Include="Helpers\CompareResultHelper.cs" />
151152
<Compile Include="IO\GetInvalidPathCharsAndGetInvalidFileNameCharsSamples01.cs" />

0 commit comments

Comments
 (0)