Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created March 22, 2017 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ufcpp/61e9a50af1469d11b9764ae1d0e06c5e to your computer and use it in GitHub Desktop.
Save ufcpp/61e9a50af1469d11b9764ae1d0e06c5e to your computer and use it in GitHub Desktop.
.NET Framework 2.0で動くC# 7.0コード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
namespace ConsoleAppFx2_0
{
class Program
{
static void Main(string[] args)
{
var inputs = new[] { A(), B(), C(), D() };
var outputs =
from x in inputs
where x.line % 2 == 1
select $"line: {x.line}, name: {x.name}";
foreach (var x in outputs)
{
Console.WriteLine(x);
}
}
static (int line, string name) CallerInfo(
[CallerLineNumber] int line = 0,
[CallerMemberName] string name = null)
=> (line, name);
static (int line, string name) A() => CallerInfo();
static (int line, string name) B() => CallerInfo();
static (int line, string name) C() => CallerInfo();
static (int line, string name) D() => CallerInfo();
}
}
namespace System
{
public delegate void Action();
public delegate TResult Func<T1, TResult>(T1 x1);
public struct ValueTuple
{
public static ValueTuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2) => new ValueTuple<T1, T2>(item1, item2);
}
public struct ValueTuple<T1, T2>
{
public T1 Item1;
public T2 Item2;
public ValueTuple(T1 item1, T2 item2) { Item1 = item1; Item2 = item2; }
}
}
namespace System.Linq
{
public static class Enumerable
{
public static IEnumerable<U> Select<T, U>(this IEnumerable<T> source, Func<T, U> filter)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (filter == null) throw new ArgumentNullException(nameof(filter));
IEnumerable<U> f()
{
foreach (var x in source) yield return filter(x);
}
return f();
}
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
IEnumerable<T> f()
{
foreach (var x in source) if (predicate(x)) yield return x;
}
return f();
}
}
}
namespace System.Runtime.CompilerServices
{
public sealed class ExtensionAttribute : Attribute { }
public sealed class CallerLineNumberAttribute : Attribute { }
public sealed class CallerMemberNameAttribute : Attribute { }
public sealed class TupleElementNamesAttribute : Attribute
{
public TupleElementNamesAttribute(string[] transformNames) => TransformNames = transformNames;
public IList<string> TransformNames { get; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment