Skip to content

Instantly share code, notes, and snippets.

@mapfel
Last active September 4, 2019 08:18
Show Gist options
  • Save mapfel/6af7a3a398d56ccd5a7291e8eefb23f1 to your computer and use it in GitHub Desktop.
Save mapfel/6af7a3a398d56ccd5a7291e8eefb23f1 to your computer and use it in GitHub Desktop.
Nice C# constructs
// Kombinieren von Werten in einer flagged Enumeration
[Flags]
public enum DefaultValueHandling
{
  Include = 0,
  Ignore = 1,
  Populate = 2,
  IgnoreAndPopulate = Ignore | Populate
}
// Instead
public LibraryService(IBookRepository bookRepository, IAuthorRepository authorRepository)
{
if (bookRepository == null)
throw new ArgumentNullException("bookRepository");
if (authorRepository == null)
throw new ArgumentNullException("authorRepository");
// ...
}
// use this with nameof and Null Coalesce Operator)
public LibraryService(IBookRepository bookRepository, IAuthorRepository authorRepository)
{
this.bookRepository = bookRepository ?? throw new ArgumentNullException(nameof(bookRepository ));
this.authorRepository = authorRepository ?? throw new ArgumentNullException(nameof(authorRepository));
}
// C# 7.0
switch(shape)
{
case Circle c:
WriteLine($"circle with radius {c.Radius}");
break;
case Rectangle s when (s.Length == s.Height):
WriteLine($"{s.Length} x {s.Height} square");
break;
case Rectangle r:
WriteLine($"{r.Length} x {r.Height} rectangle");
break;
default:
WriteLine("<unknown shape>");
break;
case null:
throw new ArgumentNullException(nameof(shape));
}
// C# 6.0
switch(o.GetType().Name)
{
case nameof(AType):
break;
case nameof(BType):
break;
}
// C# 5.0
switch(o.GetType().Name)
{
case "AType":
break;
}
// C# switch on type
// https://stackoverflow.com/questions/4478464/c-sharp-switch-on-type
// https://stackoverflow.com/questions/7252186/switch-case-on-type-c-sharp
var @switch = new Dictionary<Type, Action> {
{ typeof(Type1), () => ... },
{ typeof(Type2), () => ... },
{ typeof(Type3), () => ... },
};
@switch[typeof(MyType)]();
// How to use switch-case on a Type?
// https://stackoverflow.com/questions/7542793/how-to-use-switch-case-on-a-type
var typeProcessorMap = new Dictionary<Type, Delegate>
{
{ typeof(int), new Action<int>(i => { /* do something with i */ }) },
{ typeof(string), new Action<string>(s => { /* do something with s */ }) },
};
void ValidateProperties(object o)
{
var t = o.GetType();
typeProcessorMap[t].DynamicInvoke(o); // invoke appropriate delegate
}
// Switching on Types
// https://blogs.msdn.microsoft.com/jaredpar/2008/05/16/switching-on-types/
// Summary:
// TypeSwitch is designed to prevent redundant casting and give a syntax that is similar to a normal switch/case statement.
// For example, here is TypeSwitch in action on a standard Windows form event
public static DecoderType GetDecoderType(Decoder decoder)
{
switch (decoder)
{
case BarcoMonitorWallDecoder _:
return DecoderType.BarcoMonitorWall;
...
default:
throw new ArgumentOutOfRangeException(nameof(decoder), decoder, $"Could not resolve DecoderType for {decoder} with type {decoder.GetType()}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment