Skip to content

Instantly share code, notes, and snippets.

@DoCode
Created September 15, 2015 12:03
Show Gist options
  • Save DoCode/437fc93f73be3a3abaae to your computer and use it in GitHub Desktop.
Save DoCode/437fc93f73be3a3abaae to your computer and use it in GitHub Desktop.
c#6 features work with net2
using System;
using System.Collections.Generic;
using static System.Math;
namespace System.Runtime.CompilerServices
{
[AttributeUsage( AttributeTargets.Method )]
public class ExtensionAttribute : Attribute
{
}
}
namespace CSharp6andNET2
{
internal class Program
{
public delegate bool Filter( ArgumentException ex, string argName );
public bool DoFilter( Filter test, ArgumentException ex, string argName )
{
return test( ex, argName );
}
private static void Main( string[] args )
{
Filter x = ( ArgumentException ex, string argName ) => ex.ParamName == argName;
for ( int count = 0; count < 2; count++ )
{
try
{
ExceptionFilterTest( count == 0 );
}
catch ( ArgumentException e ) when (x( e, "argumentName" ))
{
Console.WriteLine( "Logged filtered exception" );
var test = new TestClass();
Console.WriteLine(
$"{nameof( test.Count )}: {test.Count}, {nameof( test.Count2 )}: {test.Count2}{nameof( test.Count3 )}: {test.Count3}, {nameof( test.Count4 )}: {test.Count4( 1 )}" );
var list = new List<int>
{
"9",
"25",
"36",
16,
4,
64
};
Console.WriteLine( "List" );
foreach ( var n in list )
{
Console.WriteLine( Sqrt( n ) );
}
var dictionary = new Dictionary<int, string>
{
[ 4 ] = "Test",
[ 2 ] = null,
[ 45 ] = null,
[ 34 ] = null,
[ 200 ] = null,
[ 16 ] = "Another test"
};
foreach ( var k in dictionary )
{
Console.WriteLine( $"{k.Key}: {k.Value?.Substring( 0, 3 )}" );
}
}
catch ( Exception )
{
Console.WriteLine( "Logged exception" );
}
}
}
private static void ExceptionFilterTest( bool filterable )
{
if ( filterable ) throw new ArgumentException( "Exception", "argumentName" );
throw new Exception( "Exception" );
}
}
internal static class Extensions
{
public static void Add( this List<int> list, string value )
{
list.Add( Int32.Parse( value ) );
}
}
internal class TestClass
{
public int Count { get; } = 6;
public int Count2 { get; set; } = 6;
public int Count3 => 6;
public int Count4( int x ) => x + 6;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment