Skip to content

Instantly share code, notes, and snippets.

@davidarobinson
davidarobinson / UserDefinedException.cs
Created May 26, 2019 22:30
An example of using user defined exceptions
void Main()
{
Console.WriteLine("Do you want to continue? Enter Y for Yes or N for No");
var answer = Console.ReadLine();
if (answer.ToUpper() != "Y" && answer.ToUpper() != "N")
{
throw new InvalidAnswerException();
}
}
@davidarobinson
davidarobinson / autoproperties.linq
Last active May 26, 2019 22:20
A linqpad file demonstrating the differences between C#6 read-only auto-properties and previous private set properties
void Main()
{
Person firstPerson=new Person(new DateTime(2001, 10, 16));
// Unrestricted access to FirstName and LastName
firstPerson.FirstName="John";
firstPerson.LastName="Jones";
// Cannot access Date Of Birth, other than via consructor or within class membes internally
//firstPerson.DateOfBirth= new DateTime(1926, 03, 27);
@davidarobinson
davidarobinson / BuilderPattern.linq
Last active May 26, 2019 22:19
An example of the Builder Pattern, this file can be run in LinqPad. It is designed with a fluent interface, meaning that the methods return the builder and can be chained together. Objects can be created of different state to be used in unit test.
void Main()
{
Console.WriteLine(new PersonBuilder().WithValidDetails().Build());
Console.WriteLine(new PersonBuilder().WithValidDetails().WithValidAddress().Build());
}
public class Person{
public string FirstName {get;set;}
public string LastName {get;set;}
public Address Address{get;set;}
@davidarobinson
davidarobinson / serialization.cs
Last active April 9, 2018 19:41
A quick example of how to serialize/deserialize a class - Use Linqpad
/*
Required Namespace
System.Xml.Serialization
*/
public class Person
{
public string Name {get; set;}
public int Age {get;set;}
public Address Address{get;set;}