static void MultipleLegs(PetStruct petStruct, PetClass petClass) { petStruct.Legs = petStruct.Legs * 2; petClass.Legs = petClass.Legs * 2; Console.WriteLine("Internal Method - A " + petStruct.Type + " has " + petStruct.Legs + " legs"); Console.WriteLine("Internal Method - A " + petClass.Type + " has " + petClass.Legs + " legs"); }
static void Main(string[] args) { PetStruct dog; dog.Type = PetType.Dog; dog.HasFur = true; dog.Legs = 4; PetClass duck = new PetClass { Type = PetType.Duck, HasFur = false, Legs = 2 }; Console.WriteLine("A " + dog.Type + " has " + dog.Legs + " Legs."); Console.WriteLine("A " + duck.Type + " has " + duck.Legs + " Legs."); Console.ReadLine(); }
static void Main(string[] args) { // int[5] tells us there will be 5 items in the array int[] intArray = new int[5]; string[] strArray = new string[5]; // storing items in the arrays int[] populatedIntArray = new int[] { 0, 1, 2, 3, 4, 5 }; string[] populatedStrArray = new string[] { "One", "Two", "Three" }; intArray[0] = 5; intArray[2] = 15; int firstVal = intArray[0]; // initializing two arrays with three items each int[,] multiInt = new int[2, 3]; int[,] multiPopulatedInt = { { 1, 2, 3 }, { 5, 6, 7 } }; int firstMultiVal = multiPopulatedInt[0, 0]; int secondMultiVal = multiPopulatedInt[1, 2]; // lists don't need to have the number of items declared List <string> listOfStrings = new List <string>(); listOfStrings.Add("First String"); listOfStrings.Insert(0, "Inserted String"); listOfStrings.Remove("First String"); listOfStrings.RemoveAt(0); listOfStrings.Sort(); var theFirstStrIs = listOfStrings[0]; Dictionary <string, string> names = new Dictionary <string, string>(); names.Add("James", "Bond"); names.Add("Money", "Penny"); Console.WriteLine("The name is " + names["James"]); names.Remove("James"); PetStruct dog = new PetStruct(); dog.Type = PetType.Dog; dog.HasFur = true; dog.Legs = 4; PetClass duck = new PetClass(); duck.Type = PetType.Duck; duck.HasFur = false; duck.Legs = 2; Console.WriteLine("a " + dog.Type + " has " + dog.Legs + " legs"); Console.WriteLine("a " + duck.Type + " has " + duck.Legs + " legs"); MultipleLegs(dog, duck); Console.WriteLine("a " + dog.Type + " has " + dog.Legs + " legs"); Console.WriteLine("a " + duck.Type + " has " + duck.Legs + " legs"); List <PetClass> pets = new List <PetClass>(); pets.Add(new PetClass { HasFur = false, Legs = 2, Name = "Donald", Type = PetType.Duck }); pets.Add(new PetClass { HasFur = true, Legs = 4, Name = "Pluto", Type = PetType.Dog }); List <PetClass> results = (from p in pets where p.Type == PetType.Dog select p).ToList(); results = pets.Where(p => p.Type == PetType.Dog).ToList(); Console.ReadLine(); }
static void Main(string[] args) { // Start of the block Arrays and collections int[] intArray = new int[5]; string[] stringArray = new string[5]; int[] populatedArray = new int[] { 0, 1, 2, 3, 4, 5 }; string[] populatedStringArray = new string[] { "One", "Two", "Three" }; intArray[0] = 5; intArray[2] = 15; int firstValue = intArray[0]; int[,] multiInt = new int[2, 3]; // 2 in the first column, 3 in the second int[,] multiPopulatedInt = { { 1, 2, 3 }, { 5, 6, 7 } }; int firstMultiValue = multiPopulatedInt[0, 0]; // value would be 1 int secondMultiValue = multiPopulatedInt[1, 2]; // value 7 List <string> listOfStrings = new List <string>(); listOfStrings.Add("fist String"); // add to the end of List listOfStrings.Insert(0, "Inserted String"); // insert based on index listOfStrings.Remove("first String"); // removing based on content listOfStrings.Remove("0"); // removing based in index listOfStrings.Sort(); var theFirstString = listOfStrings[0]; Dictionary <string, string> names = new Dictionary <string, string>(); names.Add("James", "Bond"); names.Add("Money", "Penny"); names.Remove("James"); // End of the block Arrays and collections PetStruct dog = new PetStruct(); dog.Type = PetType.Dog; dog.HasFur = true; dog.Legs = 4; PetClass duck = new PetClass(); duck.Type = PetType.Duck; duck.HasFur = false; duck.Legs = 2; Console.WriteLine("A " + dog.Type + " has " + dog.Legs + " legs."); Console.WriteLine("A " + duck.Type + " has " + duck.Legs + " legs."); MultipleLegs(dog, duck); Console.WriteLine("A " + dog.Type + " has " + dog.Legs + " legs."); Console.WriteLine("A " + duck.Type + " has " + duck.Legs + " legs."); List <PetClass> pets = new List <PetClass>(); //PetClass mydog = new PetClass(); // a way to create the object // Another way to create the object in the List using LINQ pets.Add(new PetClass { HasFur = false, Legs = 2, Name = "Donald", Type = PetType.Duck }); // frist item of the list pets.Add(new PetClass { HasFur = true, Legs = 4, Name = "Pluto", Type = PetType.Dog }); // second item List <PetClass> results = (from p in pets where p.Type == PetType.Dog select p).ToList(); //<PetClass> results = (from p in pets // where p.Name == "Pluto" // select p).FirstOrDefault(); // Another way using Lambda PetClass result = pets.FirstOrDefault(p => p.Type == PetType.Dog); // when returned a sigle item Console.WriteLine("found " + results.Count + " Dogs"); Console.ReadLine(); }
static void Main(string[] args) { int[] intArray = new int[5]; string[] stringArray = new string[10]; int[] populatedIntArray = new int[] { 0, 1, 2, 3, 4, 5 }; string[] populatedStringArray = new string[] { "Bob", "John", "Ron" }; intArray[0] = 5; intArray[2] = 15; int firstvalue = intArray[0]; int[,] multiInt = new int[2, 3]; int[,] multiPopulatedInt = { { 1, 2, 3 }, { 3, 4, 5 } }; int firstMultivalue = multiPopulatedInt[0, 0]; //value would be 1 int secondMultivalue = multiPopulatedInt[1, 2]; //value would be 5 List <string> listOfString = new List <string>(); listOfString.Add("First string added to list"); listOfString.Insert(0, "Inserted to the top of the list"); listOfString.Remove("First string added to list"); listOfString.RemoveAt(0); listOfString.Sort(); var theFirstStringis = listOfString[0]; Dictionary <string, string> names = new Dictionary <string, string>(); names.Add("James", "Bonds"); names.Add("lebron", "James"); Console.WriteLine("The name is " + names["James"]); names.Remove("James"); PetStruct dog = new PetStruct(); dog.Type = PetType.Dog; dog.Legs = 4; dog.HasFur = true; dog.Name = "Doggy"; PetClass duck = new PetClass(); duck.Name = "Ugly"; duck.Type = PetType.Duck; duck.Legs = 2; duck.HasFur = false; //Outside method Console.WriteLine("a " + dog.Type + " has " + dog.Legs + " legs "); Console.WriteLine("a " + duck.Type + " has " + duck.Legs + " legs "); MultipleLegs(dog, duck); //Outside method Console.WriteLine("a " + dog.Type + " has " + dog.Legs + " legs "); Console.WriteLine("a " + duck.Type + " has " + duck.Legs + " legs "); Console.ReadLine(); }