static void Main(string[] args) { var grumpyBear = new GrumpyBear(); var tenderHeart = new TenderHeart(); var tinderHeart = new TinderHeart(); var careBears = new List <CareBearBase> { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Kelsey"); bear.Stare(); } //Linq stuff var namesThatStartWithT = careBears.Where(bear => bear.Name.StartsWith('T')); var careBearNames = careBears.Select(bear => bear.Name); var careBearNamesThatStartWithT = careBears .Where(bear => bear.Name.StartsWith('T')) .Select(bear => bear.Name); var anyTinderHearts = careBears.Any(bear => bear is TinderHeart); // returns true or false var allTinderHearts = careBears.All(bear => bear is TinderHeart); // exits as soon as it finds a beart that is not a TinderHeart }
static void Main(string[] args) { var definitions = new Dictionary <string, List <string> >(); definitions.Add("martin", new List <string> { "smart", "strong", "important" }); Console.WriteLine(string.Join("you is ", definitions["martin"])); var grumpyBear = new GrumpyBear(); var tenderHeart = new TenderHeart(); var tinderHeart = new TinderHeart(); var careBears = new List <CareBearBase> { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Nathan"); bear.Stare(); } var namesThatStartWithT = careBears.Where(bear => bear.Name.StartsWith('T')); var careBearNames = careBears.Select(bears => bears.Name); var careBearNamesThatStartwithT = careBears .Where(bear => bear.Name.StartsWith('T')) .Select(bear => bear.Name); var anyBears = careBears.Any(); var anyTinderHearts = careBears.Any(bear => bear is TinderHeart); var allTinderHearts = careBears.All(bear => bear is TinderHeart); var firstBear = careBears.First(); var firstBearStartingWithT = careBears.First(bear => bear.Name.StartsWith('T')); // Throws an exception: // var blowsUp = careBears.First(bear => bear.Name.StartsWith('W')); var works = careBears.FirstOrDefault(bear => bear.Name.StartsWith('W')); var skippedGrumpy = careBears.Skip(1).Take(2); var numberOfCharacters = careBears.Sum(bear => bear.Name.Length); var orderedBears = careBears.OrderByDescending(bear => bear.BellyBadge); // Bedtime Bear, Birthday Bear, Cheer Bear, Friend Bear, Funshine Bear, Good Luck Bear, Grumpy Bear, Love-a-lot Bear, Tenderheart Bear, and Wish Bear. }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var definitions = new Dictionary <string, List <string> >(); definitions.Add() var grumpyBear = new GrumpyBear(); var tenderHeart = new TenderHeart(); var tinderHeart = new TinderHeart(); var careBears = new List <CareBearBase> { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Crystal"); bear.Stare(); } var namesThatStartWIthT = careBears.Where(bear => bear.Name.StartsWith('T')); var careBearNames = careBears.Select(bear => bear.Name); var careBearNamesThatStartWithT = careBears .Where(bear => bear.Name.StartsWith('T')) .Select(bear => bear.Name); // any returns if any one matches - returns true - as soon as it finds a match it stops var anyTinderHearts = careBears.Any(bear => bear is TinderHeart); // all only returns true if every bear is a tinderheart var allTinderHearts = careBears.All(bear => bear is TinderHeart); var otherList = new List <CareBearBase>(); var firstBear = careBears.First(); var firstBearStartingWithT = careBears.First(bear => bear.Name.StartsWith('T')); //var blowsUp = careBears.First(bear => bear.Name.StartsWith('W')); // below will just return "null" var works = careBears.FirstOrDefault(bear => bear.Name.StartsWith('W')); var skippedGrumpy = careBears.Skip(1).Take(2); var numberOfCharacters = careBears.Max(bear => bear.Name.Length); var orderedBears = careBears.OrderBy(bear => bear.BellyBadge); var orderedBears2 = careBears.OrderByDescending(bear => bear.BellyBadge); }
static void Main(string[] args) { var definitions = new Dictionary <string, List <string> >(); definitions.Add("martin", new List <string> { "smart", "strong", "important" }); Console.WriteLine(string.Join(", ", definitions["martin"].Where(word => word.StartsWith('s')))); var grumpyBear = new GrumpyBear(); var tenderHeart = new TenderHeart(); var tinderHeart = new TinderHeart(); var careBears = new List <CareBearBase> { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Nathan"); bear.Stare(); } var namesThatStartWithT = careBears.Where(bear => bear.Name.StartsWith('T')); var careBearNames = careBears.Select(bear => bear.Name); var careBearNamesThatStartWithT = careBears .Where(bear => bear.Name.StartsWith('T')) .Select(bear => bear.Name); var anyTinderHearts = careBears.Any(bear => bear is TinderHeart); //returns true after first match //delegates or lambtha expression var allTinderHearts = careBears.All(bear => bear is TinderHeart); // returns true only if every bear matches var firstBear = careBears.First(); //empty param passes actualy first or pass an argument var firstBearStartingWithTe = careBears.First(bear => bear.Name.StartsWith("Te")); var otherList = new List <CareBearBase>(); //var blowsUp = otherList.First(); // throws an exception because there is no first item in the list var doesntBlowUp = otherList.FirstOrDefault(); //outputs null value var skipGrumpy = careBears.Skip(1).Take(2); // throws an exception if your list has less than 3 items var longestName = careBears.Max(bear => bear.Name.Length); var orderBears = careBears.OrderBy(bear => bear.BellyBadge); //orders alphabetically or OrderByDescending for reverse //Bedtime Bear, Birthday Bear, Cheer Bear, Friend Bear, Funshine Bear, Good Luck Bear, Grumpy Bear, Love-a-lot Bear, Tenderheart Bear, and Wish Bear Console.ReadKey(); ; }
static void Main(string[] args) { var grumpyBear = new GrumpyBear(); var tenderHeart = new TenderHeart(); tenderHeart.Hug(grumpyBear); var tinderHeart = new TinderHeart(); var careBears = new List <CareBearBase> { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Maria"); bear.Stare(); } //these are filter and transformation statements: .select and .where var namesThatStartWithT = careBears.Where(bear => bear.Name.StartsWith('T')); var careBearNames = careBears.Select(bear => bear.Name); var careBearNamesThatStartWithT = careBears .Select(bear => bear.Name) .Where(name => name.StartsWith('T')); //the fact arrow functions: called Lambda expressions or delegate expressions //they will "short circut": .Any will stop after finding one bear that is not a tinderheart //.All will stop at the first tinderheart found var anyTinderHeartBears = careBears.Any(bear => bear is TinderHeart); var allTinderHeartBears = careBears.All(bear => bear is TinderHeart); var firstBear = careBears.First(); var firstBearStartingWithT = careBears.First(bear => bear.Name.StartsWith("T")); //var blowsUp = careBears.First(bear => bear.Name.StartsWith("W")); --> throws and exceptions var works = careBears.FirstOrDefault(bear => bear.Name.StartsWith("W")); //returns as null var skippedGrumpy = careBears.Skip(1).Take(2); //skips the 1st one, but will returnt he next 2 things var numOfCharacters = careBears.Sum(bear => bear.Name.Length); //aslo .Max var orderedBears = careBears.OrderBy(bear => bear.BellyBadge); //Bedtime Bear, Birthday Bear, Cheer Bear, Friend Bear, Funshine Bear, Good Luck Bear, Grumpy Bear, Love - a - lot Bear, Tenderheart Bear, and Wish Bear Console.ReadKey(); }
static void Main(string[] args) { var definitions = new Dictionary <string, List <string> >(); definitions.Add("martin", new List <string> { "smart", "strong", "important" }); var grumpyBear = new GrumpyBear(); var tenderHeart = new TenderHeart(); var tinderHeart = new TinderHeart(); var careBears = new List <CareBearBase> { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Randy"); bear.Stare(); } //this is like JavaScripts filter var namesThatStartWithT = careBears.Where(BearColor => BearColor.Name.StartsWith('T')); var careBearNames = careBears.Select(bear => bear.Name); var careBearNamesThatStartWithT = careBears .Where(bear => bear.Name.StartsWith('T')) .Select(bear => bear.Name); var anyTinderHearts = careBears.Any(bear => bear is TinderHeart); var allTinderHearts = careBears.All(bear => bear is TinderHeart); var firstBear = careBears.First(); var firstBearStartingWithT = careBears.First(bear => bear.Name.StartsWith('T')); //Throws an exception //var blowsUp = careBears.First(bear => bear.Name.StartsWith('W')); var works = careBears.FirstOrDefault(bear => bear.Name.StartsWith('W')); var skippedGrumpy = careBears.Skip(1).Take(2); var numberofCharacters = careBears.Sum(bear => bear.Name.Length); var orderedBears = careBears.OrderByDescending(bear => bear.BellyBadge); }
static void Main(string[] args) { var grumpyBear = new GrumpyBear(); var tenderHeart = new TenderHeart(); var tinderHeart = new TinderHeart(); var careBears = new List <CareBearBase> { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Nathan"); bear.Stare(); } var namesThatStartWithT = careBears.Where(bear => bear.Name.StartsWith('T')); var careBearNames = careBears.Select(bear => bear.Name); var careBearNamesThatStartWithT = careBears .Where(bear => bear.Name.StartsWith('T')) .Select(bear => bear.Name); var anyTinderHeart = careBears.Any(bear => bear is TinderHeart); var allTinderHearts = careBears.All(bear => bear is TinderHeart); var firstBear = careBears.First();; var firstBearStartingWithT = careBears.First(bear => bear.Name.StartsWith('T')); var otherList = new List <CareBearBase>(); //throws an exception //var blowsup = careBears.First(bear => bear.Name.StartsWith('W')); var works = careBears.FirstOrDefault(bear => bear.Name.StartsWith('W')); var skippedGrumpy = careBears.Skip(1).Take(2); var numberOfCharacters = careBears.Max(bear => bear.Name.Length); var orderedBears = careBears.OrderByDescending(bear => bear.BellyBadge); //Bedtime Bear, Birthday Bear, Cheer Bear, Friend Bear, Funshine Bear, Good Luck Bear, Grumpy Bear, Love-a-lot Bear, Tenderheart Bear, and Wish Bear. }
static void Main(string[] args) { var grumpyBear = new GrumpyBear(); var tenderHeart = new TenderHeart(); var tinderHeart = new TinderHeart(); var careBears = new List <CareBearBase> { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Nathan"); bear.Stare(); } var namesThatStartWithT = careBears.Where(bear => bear.Name.StartsWith('T')); var careBearNames = careBears.Select(bear => bear.Name); /*LINQ*/ }
static void Main(string[] args) { var grumpyBear = new GrumpyBear(); grumpyBear.Care("Nathan"); var tenderHeart = new TenderHeart(); tenderHeart.Stare(); tenderHeart.Hug(grumpyBear); var tinderHeart = new TinderHeart(); var careBears = new List <CareBearBase> { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Nathan"); bear.Stare(); } Console.ReadLine(); }
static void Main(string[] args) { var definitions = new Dictionary <string, List <string> >(); definitions.Add("monica", new List <string> { "smart", "sleepy" }); definitions["monica"].Where(word => word.StartsWith('s')); Console.WriteLine(string.Join("you are", definitions["monica"])); var grumpyBear = new GrumpyBear(); var tenderHeart = new TenderHeart(); var tinderHeart = new TinderHeart(); // a new list of CareBearBase var careBears = new List <CareBearBase>() { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Jo"); bear.Stare(); } // where similar to filter method from js // will create another list but this will only have a list of bears that start with letter T var namesThatStartWithT = careBears.Where(bear => bear.Name.StartsWith('T')); // transformation. Change shape of collection - what the collection is made of // returning collection of strings var careBearNames = careBears.Select(bear => bear.Name); // order matters // do filtering first and then transformation for efficiency var careBearNamesThatStartWithT = careBears .Where(bear => bear.Name.StartsWith('T')) .Select(bear => bear.Name); // returning boolean expression 'is there any bear that is a TinderHeart?' // finds the first 1 that is true, and only return that var anyTinderHearts = careBears.Any(bear => bear is TinderHeart); // will only return true if every care bear is a TinderHeart // All method will look until it finds something that doesn't match, and exists out var allTinderHearts = careBears.All(bear => bear is TinderHeart); // first finds the first thing on the list and returns it // can also pass in boolean expression var firstBear = careBears.First(); // give me the first name that starts with letter T var firstBearStartingWithT = careBears.First(bear => bear.Name.StartsWith('T')); // will throw an exception because no name start with W //var blowsUp = careBears.First(bear => bear.Name.StartsWith('W')); // give first one that matches or give default view of CareBearBase // in this case it will give null value var works = careBears.FirstOrDefault(bear => bear.Name.StartsWith('W')); // skip 1st item and take next 2 items // will throw an exception if List doesn't have 3 items var skippedGrumpy = careBears.Skip(1).Take(2); // sum total of all bear names var numOfChar = careBears.Sum(bear => bear.Name.Length); // order bears by BellyBadge. Alphabetical order var orderedBears = careBears.OrderBy(bear => bear.BellyBadge); // Bedtime Bear, Birthday Bear, Cheer Bear, Friend Bear, Funshine Bear, Good Luck Bear, Grumpy Bear, Love-a-lot Bear, Tenderheart Bear, and Wish Bear }
static void Main(string[] args) { // key is a string, but the value is a list of strings var definitions = new Dictionary <string, List <string> >(); // key is martin, but the value is a collection of its own definitions.Add("martin", new List <string> { "smart", "strong", "important" }); // this allows us to add something within this key definitions["martin"].Where(word => word.StartsWith('s')); Console.WriteLine(string.Join("you is ", definitions["martin"])); var grumpyBear = new GrumpyBear(); var tenderHeart = new TenderHeart(); var tinderHeart = new TinderHeart(); // to access lists, you must have using System.Collections.Generic; // we have to be explicit with the kind of things we put into Lists // the three items above have an "is a" relationship with the CareBearBase; // the curly brackets at the end of the careBears variable is called an object initializer var careBears = new List <CareBearBase> { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Nathan"); bear.Stare(); } // bread and butter are going to be where and select // LINQ - Language integrated queries: transforming and filtering collections of things // LINQ allows us to use similar JS methods like map, filter, reduce // LINQ where() is similar to JS filter() // this method does not modify the original list; the variable below will have bears whose names begin with T var namesThatStartWithT = careBears.Where(bear => bear.Name.StartsWith('T')).ToList(); var i = 0; foreach (var bear in namesThatStartWithT) { Console.WriteLine(namesThatStartWithT[i].Name); i++; } Console.ReadLine(); // transforming the List. similar to map in JS. not removing things, but changing what the collection is made of // bear.Name tells Select that we will be returning an IEnumerable of strings // IEnumerable are the lowest common denominator for all of our connections // as long as it is an ienumerable, you can use linq on it var careBearNames = careBears.Select(bear => bear.Name); // be aware of the order in which you do things because it effects your outcome // this order takes the original list and filters it down to a smaller subset before filtering the smaller group down more var careBearNamesThatStartWithT = careBears.Where(bear => bear.Name.StartsWith('T')).Select(bear => bear.Name); // lambda expressions (delegates): the fat arrow occuring inside of parentheses on linq methods; it is a function reference // the is operator allows us to name something specifically and it returns a boolean expression representing the result // Any returns true if any one of the thing in the list matches // Any: one thing must match; as soon as it finds the first of the thing, it stops looking through the list var anyTinderHearts = careBears.Any(bear => bear is TinderHeart); Console.WriteLine(anyTinderHearts); // All returns true if every bear in our list if a TinderHeart // All: looks until it finds something that doesn't match and then exits out - short circuiting var allTinderHearts = careBears.All(bear => bear is TinderHeart); Console.WriteLine(allTinderHearts); Console.ReadLine(); // when you put in your dot operator, anything with a disc and a down arrow are the linq things // aggragate is the same concept as JS reduce // first says find me the first thing in the list and return it var firstBear = careBears.First(); // entering a parameter gives the method something look for; // if you ask for something that doesn't exist, it blows up - throws an exception var firstBearStartingWithT = careBears.First(bear => bear.Name.StartsWith("Te")); // if you create a list with nothing in it var otherList = new List <CareBearBase>(); // if you ask for the first thing in a list that doesn't have anything in it, it blows up - throw an exception var blowsUp = otherList.First(); // FirstOrDefault says find the first thing that matches, or give me the default of carebearbase // a reference type, like a class, default to null var works = careBears.FirstOrDefault(bear => bear.Name.StartsWith('W')); // skip one item and then take 2 items; throws an exception if used on a list of not three things var skippedGrumpy = careBears.Skip(1).Take(2); // return the sum of all bear name lengths var numberOfCharacters = careBears.Sum(bear => bear.Name.Length); // return the number of the longest one var max = careBears.Max(bear => bear.Name.Length); // doing transformations inside for loops is not a good idea; outdated. instead, use a select // LINQ takes the place of most foreach and for loops // data transformations - like with select - are super common. get used to it // order alphabetically var orderedBears = careBears.OrderBy(bear => bear.BellyBadge); // you will know something is expensive when it costs you power; micro-optimization; the order of select and where clauses, things like that // will find anything that has bear in it var anyBears = careBears.Any(); var doNotDoThis = careBears.Where(bear => bear.Name.StartsWith('T')).Select(bear => bear.Name).Any(); }
static void Main(string[] args) { var definitions = new Dictionary <string, List <string> > (); definitions.Add("martin", new List <string> { "smart", "strong", "important" }); Console.WriteLine(string.Join("you is ", definitions["martin"])); var grumpyBear = new GrumpyBear(); var tenderHeart = new TenderHeart(); var tinderHeart = new TinderHeart(); var careBears = new List <CareBearBase> { grumpyBear, tenderHeart, tinderHeart }; foreach (var bear in careBears) { bear.Care("Nathan"); bear.Stare(); } // this is filter? var namesThatStartWithT = careBears.Where(bear => bear.Name.StartsWith('T')); // this is map var careBearName = careBears.Select(bear => bear.Name); var careBearNamesThatStartWithT = careBears .Where(bear => bear.Name.StartsWith('T')) .Select(bear => bear.Name); // are there any bears at all. do not combine where and any var anyBears = careBears.Any(); // if there are any matches; finds the first instance and returns/stops var anyTinderBears = careBears.Any(bear => bear is TinderHeart); // if every bear in our list is a TinderhEart var allTinderHearts = careBears.All(bear => bear is TinderHeart); // find me the first thing in the list and return it; var firstBear = careBears.First(); // or, I can pass in a boolean expression var firstBearStartsWithTY = careBears.First(bear => bear.Name.StartsWith("T")); // this does not work because nothing fits what you are looking for var blowsUp = careBears.First(bear => bear.Name.StartsWith("T")); // this returns null because the default value var works = careBears.FirstOrDefault(bear => bear.Name.StartsWith('W')); // lists of three with these parameters does not work. skip the first and take the next 2 var skippedGrumpy = careBears.Skip(1).Take(2); var numberOfCharacters = careBears.Sum(bear => bear.Name.Length); //var numberOfCharacters2 = careBears.Max(bear => bear.); var OrderedBears = careBears.OrderByDescending(bear => bear.BellyBadge); //Bedtime Bear, Birthday Bear, Cheer Bear, Friend Bear, Funshine Bear, Good Luck Bear, Grumpy Bear, Love-a-lot Bear, Tenderheart Bear, and Wish Bear. }