示例#1
0
        static void Main(string[] args)
        {
            //----------------------------------------------------------------------------------------------------------------------------
            // Lists
            //----------------------------------------------------------------------------------------------------------------------------
            // in arrays, can put anything in it for javascript. in C# lists can't do that, need to specify what goes in there
            // angle brackets specify what thing list contains
            // Lists are good for when you have all the same kind of thing and want to loop over things. General purpose stuff
            var names = new List <string>();

            // Add method appends new thing at the end of the list
            // Here the Add method takes a string item since that is what is specified in the angle bracket
            names.Add("COVID-19");
            names.Add("Ebola");
            names.Add("Spanish Flu");
            names.Add("SARS");
            names.Add("Rabies");
            names.Add("Stupidity");

            // Insert method adds item at the specified index
            names.Insert(0, "Shebola");

            // removes stupidity from list
            // Remove method removes actual item
            names.Remove("Stupidity");
            // RemoveAt will rempve item at that specific index
            names.RemoveAt(3);

            // add multiple items
            // adding curly brackets at the end is collection initializer
            var people = new List <string> {
                "Nathan", "Martin", "Jameka"
            };

            // AddRange method can add a whole collection to a different collection
            names.AddRange(people);

            // remove multiple things
            // RemoveRange method removes multiple things - 1st parameter is starting index and 2nd parameter is how many we want to remove
            // Count is 1 base and index is 0 base so need to subtract 1 more thing
            names.RemoveRange(names.Count - 3, 3);

            foreach (var name in names)
            {
                Console.WriteLine($"current name is {name}.");
            }

            //----------------------------------------------------------------------------------------------------------------------------
            // Dictionaries
            //----------------------------------------------------------------------------------------------------------------------------

            // want to store both name and symptoms of virus
            // Dictionaries require 2 arguments: key and value. The key is custom to me and value is whatever I define it.
            // Dictionaries are keyed by whatever you have defined it as
            // Dictionaries are good if you want to read things over and over again
            // Cannot add 2 dictionary entries with the same key. Will error out if have 2 same keys
            // Not good at ading stuff, but good at reading and finding things
            var symptoms = new Dictionary <string, string>();

            symptoms.Add("COVID-19", "Dry cough, fever, respoiratory problems, cancels everything");
            symptoms.Add("Ebola", "Fever, headache, muscle pain, and chills");
            symptoms.Add("Spanish Flu", "fever, a dry cough, fatigue, and difficulty breathing");
            symptoms.Add("SARS", "fever, dry cough, headache, muschle aches, and difficulty breathing");
            symptoms.Add("Rabies", "fever, headaches, excess salivation, muscle spasms, paralyzsis, and mental confusion");

            // get value of single thing based on key
            var covidSymptoms = symptoms["COVID-19"];

            // remove entire single item based on key
            symptoms.Remove("Ebola");

            // collection initializer
            var otherDictionary = new Dictionary <string, int> {
                { "nathan", 33 }, { "martin", 36 }
            };

            // looping over key, value pair
            // destructuring - turning into smaller things
            // turning it into the smaller things of what the dictionary is
            foreach (var(virus, symptom) in symptoms)
            {
                Console.WriteLine($"The {virus} has the following symptoms: {symptom}");
            }

            // loop over virus names and look for the name in the symptoms dictionary
            // will cause error if name doesn't exist in disctionary and don't have conditions to catch it
            foreach (var name in names)
            {
                // ContainsKey method sees if there is a key that exists in the dictionary and will return boolean value
                if (symptoms.ContainsKey(name))
                {
                    Console.WriteLine($"The {name} virus has the following symptoms: {symptoms[name]}");
                }
                else
                {
                    Console.WriteLine($"The {name}  virus is unknown...");
                }
            }

            //----------------------------------------------------------------------------------------------------------------------------
            // Queues/Stacks
            //----------------------------------------------------------------------------------------------------------------------------

            // get collections of things and get things out 1 at a time and not have it in the collection anymore
            // 1st in, 1st out = how you put it in, is also the same order how you can remove it
            // if it was Stack collection, it would be last in, first out
            var diseasesToCure = new Queue <string>();

            // adding to end of queue
            diseasesToCure.Enqueue("SARS");
            diseasesToCure.Enqueue("COVID-19");

            // remove
            // doesn't take in any arguments due to FIFO rule
            diseasesToCure.Dequeue();

            //----------------------------------------------------------------------------------------------------------------------------
            // Hashset
            //----------------------------------------------------------------------------------------------------------------------------

            // can only have value in there once
            // adding to hashsets slow, but returning values is fast
            // good for heave read operations
            var vectors = new HashSet <string>();

            // Add method takes string and hashes it, which turns it into series of bytes and use it as key for thing and string as value
            // if it sees duplicate, will see the same bytes and knows to ignore it
            vectors.Add("Airborne");
            vectors.Add("Droplet");
            vectors.Add("Bloodborne");

            //----------------------------------------------------------------------------------------------------------------------------
            var covid19 = new Virus("COVID-19", 15);

            covid19.Symptoms.Add("Fever");
            covid19.Symptoms.Add("Dry cough");
            covid19.Symptoms.Add("Cancels everything except work");
            covid19.NumOfDeathsWorldWide = 7158;

            var spanishFlu = new Virus("Spanish Flu", 11);

            spanishFlu.Symptoms.Add("Fever");
            spanishFlu.Symptoms.Add("Dry cough");
            spanishFlu.NumOfDeathsWorldWide = 50000000;

            var rabies = new Virus("Rabies", 11);

            rabies.Symptoms.Add("Fever");
            rabies.Symptoms.Add("Excess salivation");
            rabies.NumOfDeathsWorldWide = 200000;

            // creating a list of our kind of Virus class
            // added the viruses we created
            var viruses = new List <Virus> {
                covid19, spanishFlu, rabies
            };

            foreach (var virus in viruses)
            {
                Console.WriteLine($"The {virus.Name} has an incubation period of {virus.IncubationDays} and has killed {virus.NumOfDeathsWorldWide} world wide.");

                // comma separated list of virus symtptoms
                Console.WriteLine($"{virus.Name} has the following symptoms: {string.Join(", ", virus.Symptoms)}");
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            // create a new list
            var names = new List <string>();

            // add to that list by variable type
            names.Add("COVID-19");
            names.Add("Ebola");
            names.Add("Spanish Flu");
            names.Add("SARS");
            names.Add("Rabies");
            names.Add("Stupidity");
            // add a new item to the list at a certain index
            names.Insert(0, "Shebola");

            // add multiple items by collection initializer {}, and using AddRange.
            var people = new List <string> {
                "Nathan", "Martin", "Randys Kid"
            };

            names.AddRange(people);

            // remove an item by name
            names.Remove("Stupidity");
            // remove an item by index
            names.RemoveAt(3);
            // remove multiple items
            names.RemoveRange(names.Count - 3, 3); //  this removes the people collection from above.

            foreach (var name in names)
            {
                Console.WriteLine($"current name is {name}");
            }

            //------------------------------------------------------------------
            // Dictionaries below
            //------------------------------------------------------------------

            //starting a dictionary
            var symptoms = new Dictionary <string, string>();

            // addding to a dictionary
            symptoms.Add("COVID-19", "Dry cough, fever, respiratory problems");
            symptoms.Add("Ebola", "Fever, headache, muscle pain, and chills");
            symptoms.Add("Spanish Flu", " fever, a dry cough, fatigue and difficulty breathing");
            symptoms.Add("SARS", "Fever, dry cough, headache, muscle aches, and difficulty breathing");
            symptoms.Add("Rabies", "Symptoms include fever, headache, excess salivation, muscle spasms, paralysis, and mental confusion.");

            //get single thing in dictionary by the key
            var covidSymptoms = symptoms["COVID-19"];

            //remove single item by the key
            symptoms.Remove("Ebola");

            //initializing a dictionary with multiple items
            var otherDictionary = new Dictionary <string, int> {
                { "nathan", 33 }, { "martin", 36 }
            };

            // looping over the dictionary by the key and value

            foreach (var(virus, symptom) in symptoms)
            {
                Console.WriteLine($"I have the {virus} virus and my symptoms are {symptom}.");
            }

            // looping over the List and using the dictionary as well

            foreach (var name in names)
            {
                if (symptoms.ContainsKey(name))
                {
                    Console.WriteLine($"I have the {name} virus and my symptoms are {symptoms[name]}.");
                }
                else
                {
                    Console.WriteLine($"The {name} virus is unknown");
                }
            }

            //------------------------------------------------------------------
            // Queues below
            //------------------------------------------------------------------

            // instantiating the queue
            var diseasesToCure = new Queue <string>();

            // adding a queue
            diseasesToCure.Enqueue("SARS");
            diseasesToCure.Enqueue("COVID-19");
            // deleting a queue // first in, first out type collection. Whatever went in first, gets deleted first. Have to move through the collection.
            var thingToCure     = diseasesToCure.Dequeue(); // the value deleted will go into a variable. and so on.
            var nextThingToCure = diseasesToCure.Dequeue();

            //------------------------------------------------------------------
            // Hashsets below
            //------------------------------------------------------------------

            // vectors will store 1 instance of an addition, even if it is added more than once.

            var vectors = new HashSet <string>();

            vectors.Add("Airborne");
            vectors.Add("Airborne");
            vectors.Add("Droplet");
            vectors.Add("Droplet");
            vectors.Add("Bloodborne");
            vectors.Add("Bloodborne");

            // End examples.

            var covid19 = new Virus("COVID-19", 15);

            covid19.Symptoms.Add("Fever");
            covid19.Symptoms.Add("Dry Cough");
            covid19.Symptoms.Add("Cancels everything except work.");
            covid19.NumberOfDeathsWorldWide = 7150;

            var spanishFlu = new Virus("Spanish Flu", 11);

            spanishFlu.Symptoms.Add("Fever");
            spanishFlu.Symptoms.Add("Dry Cough");
            spanishFlu.NumberOfDeathsWorldWide = 50000000;

            var rabies = new Virus("Rabies", 2);

            rabies.Symptoms.Add("Fever");
            rabies.Symptoms.Add("Excess salivation");
            rabies.NumberOfDeathsWorldWide = 200000;

            var viruses = new List <Virus> {
                covid19, spanishFlu, rabies
            };

            foreach (var virus in viruses)
            {
                Console.WriteLine($"The {virus.Name} has an icubation period of {virus.IncubationDays} days and has killed {virus.NumberOfDeathsWorldWide} people.");
                Console.WriteLine($"It has the following symptoms : {string.Join(",", virus.Symptoms)}");
            }
        }
示例#3
0
        static void Main(string[] args)
        {
            var names = new List <string>();

            names.Add("COVID-19");
            names.Add("Ebola");
            names.Add("Spanish Flu");
            names.Add("SARS");
            names.Add("Rabies");
            names.Add("Stupidity");
            names.Insert(0, "Shebola");

            names.Remove("Stupidity");
            names.RemoveAt(3);

            var people = new List <string> {
                "Nathan", "Marting", "Randy's Kid"
            };

            names.AddRange(people);

            names.RemoveRange(names.Count - 3, 3);

            foreach (var name in names)
            {
                Console.WriteLine($"current name is {name}");
            }

            //----------------------------------------------------------------------------
            //Dictoniaries
            //----------------------------------------------------------------------------

            var symptoms = new Dictionary <string, string>();

            symptoms.Add("COVID-19", "Dry cough, fever, respiratory problems");
            symptoms.Add("Ebola", "Fever, headache, muscle pain, and chills");
            symptoms.Add("Spanish Flu", " fever, a dry cough, fatigue and difficulty breathing");
            symptoms.Add("SARS", "Fever, dry cough, headache, muscle aches, and difficulty breathing");
            symptoms.Add("Rabies", "Symptoms include fever, headache, excess salivation, muscle spasms, paralysis, and mental confusion.");

            var covidSymptoms = symptoms["COVID-19"];

            symptoms.Remove("Ebola");

            var otherDictionary = new Dictionary <string, int> {
                { "nathan", 33 }, { "martin", 36 }
            };

            foreach (var(virus, symptom) in symptoms)
            {
                Console.WriteLine($"The {virus} virus has the following symptoms: {symptom}");
            }

            foreach (var name in names)
            {
                if (symptoms.ContainsKey(name))
                {
                    Console.WriteLine($"The {name} virus has the following symptoms: {symptoms[name]}");
                }
                else
                {
                    Console.WriteLine($"The {name} virus is unknown...");
                }
            }

            //--------------------------------------------------------------------------------------------------------------------
            // Queues
            //--------------------------------------------------------------------------------------------------------------------

            var diseasesToCure = new Queue <string>();

            diseasesToCure.Enqueue("SARS");
            diseasesToCure.Enqueue("COVID-19");

            var thingToCure     = diseasesToCure.Dequeue();
            var nextThingToCure = diseasesToCure.Dequeue();

            //--------------------------------------------------------------------------------------------------------------------
            // Hashset
            //--------------------------------------------------------------------------------------------------------------------

            var vectors = new HashSet <string>();

            vectors.Add("Airborne");
            vectors.Add("Airborne");
            vectors.Add("Airborne");
            vectors.Add("Airborne");
            vectors.Add("Airborne");
            vectors.Add("Droplet");
            vectors.Add("Droplet");
            vectors.Add("Droplet");
            vectors.Add("Droplet");
            vectors.Add("Bloodborne");
            vectors.Add("Bloodborne");
            vectors.Add("Bloodborne");
            vectors.Add("Bloodborne");
            vectors.Add("Bloodborne");

            var covid19 = new Virus("COVID-19", 15);

            covid19.Symptoms.Add("Fever");
            covid19.Symptoms.Add("Dry Cough");
            covid19.Symptoms.Add("Cancels everything except work");
            covid19.NumberOfDeathsWorldWide = 7158;

            var spanishFlu = new Virus("Spanish Flu", 11);

            spanishFlu.Symptoms.Add("Fever");
            spanishFlu.Symptoms.Add("Dry Cough");
            spanishFlu.NumberOfDeathsWorldWide = 50000000;

            var rabies = new Virus("Rabies", 11);

            rabies.Symptoms.Add("Fever");
            rabies.Symptoms.Add("Excess Salivation");
            rabies.NumberOfDeathsWorldWide = 200000;

            var viruses = new List <Virus> {
                covid19, spanishFlu, rabies
            };

            foreach (var virus in viruses)
            {
                Console.WriteLine($"The {virus.Name} has an incubation period of {virus.IncubationDays} and has killed {virus.NumberOfDeathsWorldWide} wombats.");
                Console.WriteLine($"It has the following symptoms: {string.Join(", ", virus.Symptoms)}");
            }
        }
示例#4
0
        static void Main(string[] args)
        {
            var names = new List <string>();

            //add items to the list one at a time
            names.Add("COVID-19");
            names.Add("Ebola");
            names.Add("Spanish Flu");
            names.Add("SARS");
            names.Add("Rabies");
            names.Add("Stupidity");
            names.Insert(0, "Shebola");

            //remove items from the list. Lists are keyed by their index.
            names.Remove("Stupidity");

            //removes at a certain index
            names.RemoveAt(3);

            //add multiple items (called a collection initalizer, so I don't need the .Add stuff)
            var people = new List <string> {
                "Nathan", "Martin", "Randy's Kid"
            };

            names.AddRange(people); // or

            // names.AddRange(new List<string> { "Nathan", "Martin", "Randy's Kid"});

            //remove multiple items
            names.RemoveRange(names.Count - 3, 3);

            foreach (var name in names)
            {
                Console.WriteLine($"current name is {name}");
            }

            //Dictionarys- specialized collections. Takes a key and value. Differnt from a list

            var symptoms = new Dictionary <string, string> ();

            //add single items
            symptoms.Add("COVID-19", "Dry cough, fever, respiratory problems");
            symptoms.Add("Ebola", "Fever, headache, muscle pain, and chills");
            symptoms.Add("Spanish Flu", "Fever, dry cough, fatigue, can't breath");
            symptoms.Add("SARS", "fever, cough, headache, pains");
            symptoms.Add("Spanish Flue", "fever, headache, excess salivation, musslce spasms, paralysis, and mental confusion");

            //get single thing
            var covidSymptoms = symptoms["COVID-19"];

            //remove single item- BELOW Is just removing the key
            symptoms.Remove("Ebola");

            //collection initializer
            var otherDictionary = new Dictionary <string, int> {
                { "nathan", 33 }, { "martin", 36 }
            };

            //destructoring
            foreach (var(key, value) in symptoms)
            {
                Console.WriteLine($"The {key} virus has the following symptoms: {value}");
            }

            foreach (var name in names)
            {
                if (symptoms.ContainsKey(name))
                {
                    Console.WriteLine($"The {name} virus has the following symptoms {symptoms[name]}");
                }
                else
                {
                    Console.WriteLine($"The {name} virus is unknown.");
                }

                //Queues- also a collection

                var diseasesToCure = new Queue <string>();
                //use enqueue as "add"
                diseasesToCure.Enqueue("SARS");
                diseasesToCure.Enqueue("COVID-19");

                var thingsToCure = diseasesToCure.Dequeue(); //FIRST THING IN, FIRST OUT--- Stack collection is different
            }

            //Hashset- slow to write things(adding), getting things out of is fast

            var vectors = new HashSet <string>();

            vectors.Add("airborne");
            vectors.Add("droplet");
            vectors.Add("bloodborne");

            var covid19 = new Virus("COVID-19", 15);

            covid19.Symptoms.Add("Fever"); //symptoms is a property of our virus class
            covid19.Symptoms.Add("Dry Cough");
            covid19.Symptoms.Add("Cancels Everything except work");
            covid19.NumberOfDeathsWorldWide = 7158;

            var spanishFlu = new Virus("Spanish Flu", 11);

            spanishFlu.Symptoms.Add("Fever");
            spanishFlu.Symptoms.Add("Dry Cough");
            spanishFlu.NumberOfDeathsWorldWide = 50000000;

            var rabies = new Virus("rabies", 11);

            rabies.Symptoms.Add("Fever");
            rabies.Symptoms.Add("Dry Cough");
            rabies.NumberOfDeathsWorldWide = 200000;

            var viruses = new List <Virus> {
                covid19, spanishFlu, rabies
            };

            foreach (var virus in viruses)
            {
                Console.WriteLine($"The {virus.Name} has an incubation period of {virus.IncubationDays} and has killed {virus.NumberOfDeathsWorldWide} people.");
                Console.WriteLine($"It has the following symptoms: {string.Join(", ", virus.Symptoms)}");
            }
        }