Skip to content

Instantly share code, notes, and snippets.

@d630
Last active June 11, 2018 20:02
Show Gist options
  • Save d630/513b95d6d264272bbd23b2a9a74e6b3a to your computer and use it in GitHub Desktop.
Save d630/513b95d6d264272bbd23b2a9a74e6b3a to your computer and use it in GitHub Desktop.
List stuff
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace csharp
{
public class Test
{
private string foo;
private string bar;
public Test(string foo, string bar)
{
this.foo = foo;
this.bar = bar;
}
public void say()
{
Console.WriteLine(foo + " " + bar);
}
}
public class Program
{
static void stuff(List<int> l)
{
Console.WriteLine(l.Count());
Console.WriteLine(l.Min());
Console.WriteLine(l.Max());
Console.WriteLine(l.Sum());
}
static void Main(string[] args)
{
List<Test> t = new List<Test>();
t.Add(new Test("hello", "earth"));
t.Add(new Test("hello", "moon"));
t.Insert(0, null);
t.Insert(0, new Test("hello", "sun"));
t.RemoveAt(1);
// runtime error
try
{
t.RemoveAt(1111);
}
catch(ArgumentOutOfRangeException aex)
{
Console.WriteLine("Oh nooo");
Console.WriteLine(aex.Message);
}
catch(Exception ex)
{
Console.WriteLine("Mordor");
Console.WriteLine(ex);
}
finally
{
Console.WriteLine("finally");
}
foreach (Test tt in t)
tt.say();
List<int> i = new List<int>();
// i = Enumerable.Range(0, 10).ToList();
Random random = new Random();
i = Enumerable.Range(0,10).OrderBy(x => random.Next()).ToList();
foreach (int ii in i)
Console.WriteLine(ii);
stuff(i);
i.Sort();
Console.WriteLine(i[0]);
Console.WriteLine(i[i.Count() - 1]);
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment