isパターン、関係パターンと論理パターン(C# 6でのエミュレーション)
C# 6で基本的なパターンを再現します。isによる型チェック、範囲・関係チェック、小さな述語ヘルパーによる論理的な組み合わせを扱います。
「isパターン、関係パターンと論理パターン(C# 6でのエミュレーション)」はCoddyKit上の無料C# Academyレッスンです。 これはレッスン1/3です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはC# Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 C# Academyコースには全3レッスンが含まれています。
C# 6 でのパターンの考え方
目標: C# 6 で一般的なパターンを再現します。
- 型パターン: is + cast
- 関係: ガードヘルパー(例: >, <)
- 論理: 小さな述語を組み合わせます(AND/OR/NOT)
is + cast による型パターン
is と明示的なキャストを使って「束縛」するローカル変数を作り、型パターンを再現します。
using System;
public class Program
{
static string Describe(object x)
{
if (x is int)
{
int i = (int)x; // bind
return "int: " + i;
}
if (x is string)
{
string s = (string)x; // bind
return "string(len=" + (s == null ? 0 : s.Length) + ")";
}
return "other";
}
public static void Main(string[] args)
{
Console.WriteLine(Describe(5));
Console.WriteLine(Describe("hi"));
Console.WriteLine(Describe(3.14));
}
}
ガードとしての関係「パターン」
関係チェック用の小さなガードヘルパーを作成します。パターンのように読め、再利用も簡単です。
using System;
public class Program
{
static bool IsBetween(int x, int minInclusive, int maxExclusive)
{
return x >= minInclusive && x < maxExclusive;
}
public static void Main(string[] args)
{
int v1 = 10, v2 = -5, v3 = 100;
Console.WriteLine(IsBetween(v1, 0, 100)); // True
Console.WriteLine(IsBetween(v2, 0, 100)); // False
Console.WriteLine(IsBetween(v3, 0, 100)); // False
}
}
ガードの論理合成
小さな述語を組み合わせて、論理パターン(AND、OR、NOT)を再現します。
using System;
public class Program
{
static bool IsEven(int n) { return n % 2 == 0; }
static bool IsPositive(int n) { return n > 0; }
static bool And(Func<int,bool> a, Func<int,bool> b) { return a(4) && b(4); } // example composition (demo)
// For general composition:
static Func<int,bool> AND(Func<int,bool> a, Func<int,bool> b)
{
return delegate(int n) { return a(n) && b(n); };
}
static Func<int,bool> OR(Func<int,bool> a, Func<int,bool> b)
{
return delegate(int n) { return a(n) || b(n); };
}
static Func<int,bool> NOT(Func<int,bool> a)
{
return delegate(int n) { return !a(n); };
}
public static void Main(string[] args)
{
Func<int,bool> evenAndPositive = AND(IsEven, IsPositive);
Console.WriteLine(evenAndPositive(4)); // True
Console.WriteLine(evenAndPositive(-2)); // False
Console.WriteLine(NOT(IsEven)(3)); // True (3 is not even)
}
}
型と関係のエミュレーション
is + キャストで値を束縛し、ガード条件を適用します。これは現代的な「型 + 関係」パターンの直接的な代替手段です。
using System;
public class Program
{
static string Check(object x)
{
if (x is int)
{
int i = (int)x; // bind
if (i > 0) return "positive int";
return "non-positive int";
}
return "not an int";
}
public static void Main(string[] args)
{
Console.WriteLine(Check(5)); // positive int
Console.WriteLine(Check(-3)); // non-positive int
Console.WriteLine(Check("hey")); // not an int
}
}
ヒントと制限
ヒント:
- ガードヘルパーには IsBetween、IsNonEmpty など、わかりやすい名前を付けます。
- 副作用を避け、純粋なチェックを優先します。
- 読みやすさのため、述語の合成は小さく保ちます。
制限: 新しいパターン構文のほうが短くなりますが、これらの C# 6 の手法でも同じ意図を表現できます。
型と関係のパターンをエミュレートする
まとめ
まとめ: C# 6 では is + キャストを組み合わせて値を束縛し、関係チェックには小さなガードを使用して、論理パターンには述語を合成します。
よくある質問
「isパターン、関係パターンと論理パターン(C# 6でのエミュレーション)」レッスンは無料ですか?
はい。「isパターン、関係パターンと論理パターン(C# 6でのエミュレーション)」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、C# Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 C# Academyコースには全3レッスンが含まれています。
「isパターン、関係パターンと論理パターン(C# 6でのエミュレーション)」で何を学びますか?
C# 6で基本的なパターンを再現します。isによる型チェック、範囲・関係チェック、小さな述語ヘルパーによる論理的な組み合わせを扱います。 ブラウザで直接実行するハンズオンコードでC# Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
C# Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのC# Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/3です。
「isパターン、関係パターンと論理パターン(C# 6でのエミュレーション)」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このC# Academyレッスンでコードを書いて実行できますか?
はい。すべてのC# Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- isパターン、関係パターンと論理パターン(C# 6でのエミュレーション)
- switch式とwhenによる網羅性チェック(C# 6でのエミュレーション)
- 判別共用体(record)によるドメインモデリング — C# 6でのエミュレーション