using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using System.Collections.Concurrent;
public static void Main() {
Console.WriteLine("Concurrent class");
var orders = new ConcurrentQueue<string>();
Task task1 = Task.Run(() => PlaceOrders(orders, "Robert"));
Task task2 = Task.Run(() => PlaceOrders(orders, "James"));
Task.WaitAll(task1, task2);
var tshirts = new ConcurrentDictionary<string, int>();
tshirts.TryAdd("jDays", 4);
tshirts.TryAdd("Tech Hour", 2);
foreach (var item in tshirts) {
Console.WriteLine(item.Key);
Console.WriteLine("Number of shirts in stock = {0}", tshirts.Count());
tshirts.TryAdd("PluralSight", 7);
Console.WriteLine(tshirts["JavaOne"]);
var success = tshirts.AddOrUpdate("jDays", 1, (key, oldValue) => oldValue + 1);
Console.WriteLine("jDays tshirts = {0}", success);
tshirts.TryRemove("jDays", out removed);
Console.WriteLine("Number of tshirst remaining = {0}", removed);
DisplayPurchase("rae", 9);
static void PlaceOrders(ConcurrentQueue<string> orders, string custName) {
for (int i=0; i < 5; i++) {
string orderName = string.Format("{0} wants t-shirt # {1}", custName, i+1);
orders.Enqueue(orderName);
public static void DisplayPurchase(string item, int qty) {
Console.WriteLine("Thread {0} : bought {1} of {2}", Thread.CurrentThread.ManagedThreadId, qty, item);
public class StockController {
ConcurrentDictionary<string, int> _stock = new ConcurrentDictionary<string, int>();
public void BuyStock(string item, int qty) {
_stock.AddOrUpdate(item, qty, (key, oldValue) => oldValue + qty);
Interlocked.Add(ref _totalQtyBought, qty);
public bool TrySellItem(string item) {
int newStockLevel = _stock.AddOrUpdate(item, (itemName) => {success = false; return 0;},
(itemName, oldValue) => {
Interlocked.Increment(ref _totalQtySold);
public static void Main()
DateTime todaysDate = DateTime.Now;
Console.WriteLine(DateTime.Now.ToUniversalTime());
Console.WriteLine(todaysDate.GetLegacyDate());
Console.WriteLine("Elings, Robert".GetLegacyName());
Console.WriteLine(StringExtensions.ConvertToTitleCase("hello world"));
Console.WriteLine("how are you".ConvertToTitleCase());
var custList = BuildCustomerList();
var qCust = custList.Where(c => c.LastName=="Doe");
var lambdaCust = custList.First(c=>c.ID == 2);
var builder = new Builder();
IReferenceDataSource apiSource = new ApiReferenceDataSource();
var apiSourceData = apiSource.GetItems();
foreach (var item in apiSourceData) {
Console.WriteLine(item.Code);
apiSource = new XMLReferenceDataSource();
apiSourceData = apiSource.GetItemsByCode("xml");
foreach (var item in apiSourceData) {
Console.WriteLine(item.Code);
private static void LinqOrderBy() {
var catList = GetCats().OrderBy(c=>c.Name).ThenBy(c=>c.Age);
foreach (var p in catList) {
Console.WriteLine("{0} is {1} year(s) old", p.Name, p.Age);
private static void LambdaUnionIntersect() {
List<Pet> catList = GetCats();
Pet[] dogList = GetDogs();
Console.WriteLine("====== LambdaUnionIntersect =====");
IEnumerable<Pet> concatList = catList.Union(dogList, new PetComparer());
foreach (var p in concatList) {
Console.WriteLine(p.Name);
var exp = concatList.Where(c=>c.Name=="Barley" || c.Age<=4).OrderBy(c=>c.Age);
Console.WriteLine(p.Breed);
var expAll = concatList.All(c=>c.Age>0);
Console.WriteLine(expAll);
var expAvgAge = concatList.Where(c=>c.Age > concatList.Average(a=>a.Age+1));
foreach (Pet p in expAvgAge) {
Console.WriteLine(p.Name);
private static void Select () {
var custList = BuildCustomerList();
var custTypeList = BuildCustomerTypeList();
var query = custList.Join(custTypeList,
CustomerName = c.LastName + ", " + c.FirstName,
foreach (var c in query) {
Console.WriteLine(c.CustomerName + " : " + c.CustomerType);
private static void LambdaAverage(List<Customer> cList, bool b) {
exp = cList.Average(c=>c.ID+3.5);
private static void LambdaAll(List<Customer> cList) {
var exp = cList.Any(c=>c.ID==1);
"{0} customers have ID == 1.",
exp ? "Some" : "Not all");
private static void LambdaIsEnumerable(List<Customer> cList) {
var exp = cList.AsEnumerable().TakeWhile(c=>c.ID<3);
Console.WriteLine("====");
exp = cList.AsEnumerable().TakeWhile(c=>c.LastName.Contains("o"));
Console.WriteLine(c.LastName);
private static List<CustomerType> BuildCustomerTypeList() {
var custList = new List<CustomerType>();
var cust = new CustomerType();
cust.Type = "Individual";
cust = new CustomerType(2, "Corporate");
custList.Add(new CustomerType(5, "Joint"));
private static List<Customer> BuildCustomerList() {
var custList = new List<Customer>();
var cust = new Customer();
cust = new Customer(2, "Doe", "John");
custList.Add(new Customer(4, "Wayne", "Brian"));
custList.Add(new Customer(5, "Jones", "Brent"));
private static List<Pet> GetCats()
List<Pet> cats = new List<Pet> {
new Pet() { Name="Barley", Age=8, Breed="Siamese" },
new Pet() { Name="Boots", Age=4, Breed="Calico" },
new Pet() { Name="Whiskers", Age=1, Breed="Tomcat" },
new Pet() { Name="Barley", Age=2, Breed="Mynx" },
new Pet() { Name="Whiskers", Age=2, Breed="Mexican Hairless" },
private static Pet[] GetDogs()
new Pet { Name="Barley", Age=8, Breed="Setter" },
new Pet { Name="Snoopy", Age=14, Breed="Shepard" },
new Pet { Name="Fido", Age=9, Breed="Boxer" }
public class Pet : PetComparer {
public string Name {get; set;}
public int Age {get; set;}
public string Breed { get; set; }
public Pet (string name, int age, string breed) {
public class PetComparer : IEqualityComparer<Pet> {
public bool Equals(Pet x, Pet y)
if (Object.ReferenceEquals(x, y)) return true;
return x != null && y != null && x.Name.Equals(y.Name) && x.Age.Equals(y.Age) && x.Breed.Equals(y.Breed);
public int GetHashCode(Pet obj)
int hashPetName = obj.Name == null ? 0 : obj.Name.GetHashCode();
int hashPetAge = obj.Age.GetHashCode();
return hashPetName ^ hashPetAge;
public abstract class ApiData {
public string Name = "ApiData";
public abstract class XMLData {
public string Name = "XMLData";
public class ApiReferenceDataSource : ApiData, IReferenceDataSource {
public IEnumerable<ReferenceDataItem> GetItems() {
return new List<ReferenceDataItem> {
new ReferenceDataItem { Code="api", Description = "from Api" },
new ReferenceDataItem { Code="api", Description = "from Api2" }
public class XMLReferenceDataSource : XMLData, IReferenceDataSource {
public IEnumerable<ReferenceDataItem> GetItems() {
return new List<ReferenceDataItem> {
new ReferenceDataItem { Code="xml", Description = "from XML" },
new ReferenceDataItem { Code="xml", Description = "from XML2" }
public class ReferenceDataItem {
public string Code { get; set; }
public string Description { get; set; }
public interface IReferenceDataSource {
IEnumerable<ReferenceDataItem> GetItems();
public static class IReferenceDataInterfaceExtension {
public static IEnumerable<ReferenceDataItem> GetItemsByCode(this IReferenceDataSource source, string code)
return source.GetItems().Where(c=>c.Code == code);
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Customer (int id, string lastName, string firstName){
this.FirstName = firstName;
this.LastName = lastName;
public class CustomerType {
public int ID { get; set; }
public string Type { get; set; }
public CustomerType (int id, string type){
public static class DateExtensions {
public static string GetLegacyDate(this DateTime dt) {
return dt.Year > 1930 ? dt.ToString("0yyMMdd:hhmmss") : dt.ToString("1yyMMdd:hhmmss");
public static class StringExtensions {
public static string ConvertToTitleCase(this string s) {
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
return myTI.ToTitleCase(s);
public static string GetLegacyName(this string name) {
public IEnumerable<int> BuildIntegerSequence() {
var sequence = Enumerable.Range(0,10).Select(i=> 5+(10*i));
public IEnumerable<string> BuildStringSequence() {
Random rand = new Random();
var sequence = Enumerable.Range(0,10).Select(i=>((char)('A'+rand.Next(0,26))).ToString());
public IEnumerable<int> BuildIntegerRepeat() {
var repeat = Enumerable.Repeat(1,15);
public void CompareSequences() {
var sequence1 = Enumerable.Range(0,10);
var sequence2 = Enumerable.Range(0,10).Select(i=>i*i);
var result = sequence1.Concat(sequence2);
Console.WriteLine("===== {0} =====", result.Count());
foreach (var r in result) {
result = sequence2.Concat(sequence1);
Console.WriteLine("===== {0} =====", result.Count());
foreach (var r in result) {