using System.Collections;
using System.Collections.Generic;
public static void Main()
public int? X { get; set; }
public int? Y { get; set; }
public string Name { get; set; }
static Point Sample0() => new Point().To(out var p).With(
static Point Sample01() => new Point().To(out var p).With(new object[] {
public static Point GetPoint() => new Point { Name = "Point Name" };
static string NameProperty { get; set; }
GetPoint().To(out var p).With(
p.Name.To(out nameLocal),
Console.WriteLine(nameLocal);
Console.WriteLine(NameField);
Console.WriteLine(NameProperty);
((Point)null).To(out var p)?.With(
Console.WriteLine("No exception");
public static Person GetPerson() => new Person { Subperson = new Person() { Name = "SubnameP"} };
public string Name { get; set; } = "Default Name";
public string Login { get; set; }
public DateTime Birthday { get; set; }
public int? Age { get; set; } = 32;
public Person Subperson { get; set; }
public Person GetSubperson() => new Person() { Name = "SubnameM" };
GetPerson().To(out var p).With(
p.Subperson.To(out var p0).With(
p0.Name.To(out var subpersonName0)
p.GetSubperson().To(out var p1).With(
p1.Name.To(out var subpersonName1)
Console.WriteLine(subpersonName0);
Console.WriteLine(subpersonName1);
if (GetPerson().Is(out var p)
p.Subperson.Is(out var sp)
sp.Name.Is(out var spName),
Console.WriteLine(spName);
new List<string>().Merge("A", "B", "C").ForEach(Console.WriteLine);
public static class LanguageExtensions
public static object ChangeType(this object o, Type type) =>
o == null || type.IsValueType || o is IConvertible ? Convert.ChangeType(o, type, null) : o;
public static void Let<T>(this T o) { }
public static TR Let<T, TR>(this T o, TR y) => y;
public static TR Let<T, TR>(this T o, TR y, out T x) => (x = o).Let(y);
public static T To<T>(this T o) => o;
public static T To<T>(this object o) => (T) ChangeType(o, typeof(T));
public static T To<T>(this T o, out T x) => x = o;
public static T To<T>(this object o, out T x) => x = (T) ChangeType(o, typeof(T));
public static string ToStr(this object o) => o?.ToString();
public static string ToStr(this string o) => o;
public static T As<T>(this T o) where T : class => o;
public static T As<T>(this object o) where T : class => o as T;
public static T As<T>(this T o, out T x) where T : class => x = o;
public static T As<T>(this object o, out T x) where T : class => x = o as T;
public static bool IsNull<T>(this T o) => o == null;
public static bool IsNull<T>(this T o, out T x) => (x = o) == null;
public static bool Is<T>(this T o) => o != null;
public static bool Is<T>(this object o) => o is T;
public static bool Is<T>(this T o, out T x) => (x = o) != null;
public static bool Is<T>(this object o, out T x) => (x = o is T ? (T) o : default(T)) != null;
public static bool Is<T>(this T o, T x) => Equals(o, x);
public static bool Is<T>(this object o, T x) => Equals(o, x);
public static bool Is<T>(this T? o, T x) where T : struct => Equals(o, x);
public static bool IsNot<T>(this T o, T x) => !Equals(o, x);
public static bool IsNot<T>(this object o, T x) => !Equals(o, x);
public static bool IsNot<T>(this T? o, T x) where T : struct => !Equals(o, x);
public static bool Not(this bool b) => !b;
public static T With<T>(this T o, params object[] pattern) => o;
public static TCollection Merge<TCollection, T>(this TCollection collection, params T[] items)
where TCollection : ICollection<T>, ICollection
foreach (var item in items) collection.Add(item);
public static bool AndAll(this bool o, params bool[] checker) => o && checker.All(b => b);
public static bool AndAny(this bool o, params bool[] checker) => o && checker.Any(b => b);
public static bool OrAll(this bool o, params bool[] checker) => o || checker.All(b => b);
public static bool OrAny(this bool o, params bool[] checker) => o || checker.Any(b => b);
public static bool XorAll(this bool o, params bool[] checker) => o ^ checker.All(b => b);
public static bool XorAny(this bool o, params bool[] checker) => o ^ checker.Any(b => b);
public static KeyValuePair<TKey, TValue> To<TKey, TValue>(this TKey key, TValue value) =>
new KeyValuePair<TKey, TValue>(key, value);
public static KeyValuePair<TKey, TValue> By<TKey, TValue>(this TValue value, TKey key) =>
new KeyValuePair<TKey, TValue>(key, value);
public static Switch<T> Match<T>(this T value, params object[] pattern) =>
new Switch<T>(value, pattern);
public static TR Switch<T, TR>(this Switch<T> m, Func<Switch<T>, TR> matcher) => matcher(m);
private readonly T _value;
private object[] _pattern;
public Switch(T value) => _value = value;
public Switch(T value, object[] pattern) : this(value) => _pattern = pattern;
public bool Case(params object[] pattern)
pattern = pattern ?? new object[] {null};
_pattern = _pattern ?? new object[] {_value};
for (var i = 0; i < pattern.Length && i < _pattern.Length; i++)
if (Equals(pattern[i], _pattern[i])) continue;
public bool Case<TValue>() => _value is TValue;
public bool Case(out T value) => Case<T>(out value);
public bool Case<TValue>(out TValue value) where TValue : T =>
(value = (_value is TValue).To(out var b) ? (TValue) _value : default(TValue)).Let(b);
public static T[] Array<T>(params T[] items) => items;
public static T Value<T>() where T : struct => new T();
public static T Value<T>(T v) where T : struct => v;
public static T Object<T>() where T : new() => new T();
public static T Object<T>(params object[] constructorArgs) =>
(T) Activator.CreateInstance(typeof(T), constructorArgs);
public static object Object(Type type, params object[] constructorArgs) =>
Activator.CreateInstance(type, constructorArgs);