Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# Influences Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Host
{
public class Examples
{
public void TypeInference()
{
////Simple type inference examples
// var s = "The quick brown fox jumped over the lazy brown dog";
// var y = 123;
// var z = System.Reflection.Assembly.GetExecutingAssembly();
////What does this code do?
//var varY = 5.0;
//var varZ = 5;
//Console.WriteLine(varY == varZ);
//Console.WriteLine(varY.GetType() == varZ.GetType());
////What does this code do?
//var varX = "hello world!";
//var varY = 123;
//Console.WriteLine(varX == varY.ToString());
////Another way to think about it...
//string varX = "hello world!";
//int vary = 123;
//Console.WriteLine(varX == varY);
////How about this?
// var varZ = null;
}
public void anonymousTypes()
{
//// Anonymous Types
//var x = new List<string>();
//var anon1 = new { foo = 'a' };
//var anon2 = new { foo = 'b' };
//var z = anon2;
//Console.WriteLine(anon1 == anon2);
//Console.WriteLine(anon1.GetType() == anon2.GetType());
}
public void WriteString(string stringtoWrite)
{
Console.WriteLine(stringtoWrite);
}
internal void Lambdas()
{
////Delegate format
//Action<string> myWriteString = WriteString;
//myWriteString = WriteString;
////Anonymous function format
//myStrings.ForEach(delegate(string s) { Console.WriteLine(s); });
////Lambdas
//List<string> myStrings = new List<string>();
// myStrings.ForEach( (s) => Console.WriteLine(s));
////What does this evaluate to?
//Func<string> lambdaY = delegate { return "A Simple Lambda"; };
//Func<string> lambdaX = () => "Another Simple Lambda";
//Console.WriteLine(lambdaY.GetType() == lambdaX.GetType());
}
public void LINQ()
{
IList<int> nums = new List<int> {0,1,2,3,4,5,6,7,8,9,10};
IList<int> fibs = new List<int> { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 };
IList<int> primes = new List<int> {2,3,5,7,11,13,17,19,23,29};
//// Identity operation
// var output = from num in nums
// select num;
// Console.WriteLine(output.Average());
// Console.WriteLine(output.Count());
////More Complex LINQ
// var out2 = from f in fibs
// from p in primes
// where f == p
// select new { Fib = f, Prime = p };
// Console.WriteLine(out2.Count());
////Lazy evaluation
//Lazy<string> myLazyString;
//myLazyString = new Lazy<string>(() => "abc", true);
//Console.WriteLine(myLazyString.Value);
////What does this do?
// var output = from n in nums
// where n == 5 && n == 7
// select n;
// Console.WriteLine(output.First());
// Console.WriteLine(output.FirstOrDefault());
}
public void dynamics()
{
////Dynamics example
//dynamic x = new Radio();
//Console.WriteLine(x.PlayMusic());
//Console.WriteLine(x.Play());
//Console.WriteLine(x.HelloWorld());
////What does this do?
//dynamic x = "Hi";
//dynamic y = 5;
//Console.WriteLine(x == y);
}
public void PLINQ()
{
Stopwatch stopwatch = new Stopwatch();
List<int> bigList = new List<int>();
for (int i = 0; i < 100000000; i++)
bigList.Add(i);
var parallelResult = from n in bigList.AsParallel()
select n * n;
var result = from n in bigList
select n * n;
stopwatch.Start();
var output = result.Average();
stopwatch.Stop();
Console.WriteLine("Total time serial: {0}", stopwatch.Elapsed);
stopwatch.Reset();
stopwatch.Start();
var poutput = parallelResult.Average();
stopwatch.Stop();
Console.WriteLine("Total time parallel: {0}", stopwatch.Elapsed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment