Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace TestCsharpVersion
- {
- class Program
- {
- //This should give an error since CSharp 3 does not support optional params
- private void TestOptionalParam(string myOptional = "foobar")
- {
- }
- static void Main(string[] args)
- {
- //Example from http://ericlippert.com/2009/11/12/closing-over-the-loop-variable-considered-harmful-part-one/
- //For C# version 5 this should return 100, 110, 120 and for C# version 4 or lower this should return 120, 120, 120
- var values = new List<int>() { 100, 110, 120 };
- var funcs = new List<Func<int>>();
- foreach (var v in values)
- funcs.Add(() => v);
- foreach (var f in funcs)
- Console.WriteLine(f());
- //Logic to check if we're on C# 5
- var testList = new List<string> { "a", "b" };
- var lambdas = new List<Func<string>>();
- var result = "";
- //Capture using foreach
- foreach (var test in testList)
- {
- lambdas.Add(() => test);
- }
- //Read the captured vars
- foreach (var lamdba in lambdas)
- {
- result += lamdba();
- }
- //Check if it's C# 5 behavior
- if (result == "ab")
- {
- result = "This is C# 5 or higher";
- }
- else if (result == "bb")
- {
- result = "This is C# 4 or lower";
- }
- else
- {
- result = "Unexpected result, no idea which C# version";
- }
- Console.WriteLine(result);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement