is patterns; relational & logical patterns (C# 6 emulation)
Emulate basic patterns in C# 6: type checks with is, range/relational checks, and logical composition via tiny predicate helpers.
is patterns; relational & logical patterns (C# 6 emulation) is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Pattern ideas in C# 6
Goal: Use C# 6 to mimic common patterns.
- Type pattern: is + cast
- Relational: helper guards (e.g., >, <)
- Logical: combine small predicates (AND/OR/NOT)
Type pattern via is + cast
Emulate a type pattern with is plus an explicit cast to “bind” a local variable.
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));
}
}
Relational “patterns” as guards
Create tiny guard helpers for relational checks; they read like patterns and are easy to reuse.
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
}
}
Logical composition of guards
Combine small predicates to emulate logical patterns: 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)
}
}
Type+relational emulation
Bind with is + cast, then apply a guard condition — a direct stand-in for a modern “type + relational” pattern.
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
}
}
Tips & limits
Tips:
- Name guard helpers clearly (IsBetween, IsNonEmpty, etc.).
- Prefer pure checks; avoid side effects.
- Keep predicate composition small for readability.
Limit: Newer pattern syntax is shorter, but these C# 6 techniques express the same intent.
Emulating a type + relational pattern
Recap
Recap: In C# 6, combine is + cast to bind values, use small guards for relational checks, and compose predicates for logical patterns.
Frequently asked questions
Is the “is patterns; relational & logical patterns (C# 6 emulation)” lesson free?
Yes — the full text of “is patterns; relational & logical patterns (C# 6 emulation)” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “is patterns; relational & logical patterns (C# 6 emulation)”?
Emulate basic patterns in C# 6: type checks with is, range/relational checks, and logical composition via tiny predicate helpers. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “is patterns; relational & logical patterns (C# 6 emulation)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- is patterns; relational & logical patterns (C# 6 emulation)
- switch expressions; exhaustive checks with when (C# 6 emulation)
- Domain modeling with discriminated unions (records) — C# 6 emulation