Sie sind auf Seite 1von 18

12/11/2018 Learn C# in One Video

Home
About
Business Plan »
Communication »
Dieting
Sales
Sitemap
Videos »
Web Design »

Communication »
Diet Nutritional
Flash Tutorial
How To »
Investing
iPad »
Marketing »
Most Popular
Royalty Free Photos
Sales
Web Design »

Learn C# in One Video


Posted by Derek Banas on Jul 4, 2015 in Web Design | 0 comments

After many requests, finally I finished my learn C# in one video tutorial. I cover a ton in
this tutorial including : User input,Data Types, Math, Casting, If, Switch, Ternary Operator, While, Do While,
For Loops, Foreach, Strings, Formatting Strings, StringBuilder, Arrays, Lists, Exception Handling, Converting
to Data Types, Classes, Objects, Getters, Setters, Constructors, Static, Overloading Methods, Object Initializer,
Inheritance, Calling Superclass Methods, Overriding Class Methods, Abstract Classes, Interfaces, Generics,
Enums, Structs, Anonymous Methods, Lambda Expressions, File I/O and more. The code follows the video
below.

http://www.newthinktank.com/2015/07/learn-c-one-video/ 1/18
12/11/2018 Learn C# in One Video

C# Tutorial

If you like videos like this, it helps to tell Google with a click here

Code From the Video

1 // Use using to declare namespaces and functions we wish to use


2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
7 using AnimalNS;
8  
9 /*
10 Multiline Comment
11 */
12  
13 // Delegates are used to pass methods as arguments to other methods
14 // A delegate can represent a method with a similar return type and attribute list
15 delegate double GetSum(double num1, double num2);
16  
17 // ---------- ENUMS ----------
18 // Enums are unique types with symbolic names and associated values
19  
20 public enum Temperature
21 {
22     Freeze,
23     Low,
24     Warm,
25     Boil
26 }
27  
28 // ---------- STRUCT ----------
29 // A struct is a custom type that holds data made up from different data types
30 struct Customers
31 {
32     private string name;
33     private double balance;
34     private int id;
35  
36     public void createCust(string n, double b, int i)
37     {
38         name = n;
http://www.newthinktank.com/2015/07/learn-c-one-video/ 2/18
12/11/2018 Learn C# in One Video

39         balance = b;
40         id = i;
41     }
42  
43     public void showCust()
44     {
45         Console.WriteLine("Name : " + name);
46         Console.WriteLine("Balance : " + balance);
47         Console.WriteLine("ID : " + id);
48     }
49 }
50  
51 // Give our code a custom namespace
52 namespace ConsoleApplication1
53 {
54     class Program
55     {
56         // Code in the main function is executed
57         static void Main(string[] args)
58         {
59             // Prints string out to the console with a line break (Write = No Line Break)
60             Console.WriteLine("What is your name : ");
61  
62             // Accept input from the user
63             string name = Console.ReadLine();
64  
65             // You can combine Strings with +
66             Console.WriteLine("Hello " + name);
67  
68             // ---------- DATA TYPES ----------
69  
70             // Booleans are true or false
71             bool canVote = true;
72  
73             // Characters are single 16 bit unicode characters
74             char grade = 'A';
75  
76             // Integer with a max number of 2,147,483,647
77             int maxInt = int.MaxValue;
78  
79             // Long with a max number of 9,223,372,036,854,775,807
80             long maxLong = long.MaxValue;
81  
82             // Decimal has a maximum value of 79,228,162,514,264,337,593,543,950,335
83             // If you need something bigger look up BigInteger
84             decimal maxDec = decimal.MaxValue;
85  
86             // A float is a 32 bit number with a maxValue of 3.402823E+38 with 7 decimals of pre
87             float maxFloat = float.MaxValue;
88  
89             // A float is a 32 bit number with a maxValue of 1.797693134E+308 with 15 decimals o
90             double maxDouble = double.MaxValue;
91  
92             // You can combine strings with other values with +
93             Console.WriteLine("Max Int : " + maxDouble);
94  
95             // The dynamic data type is defined at run time
96             dynamic otherName = "Paul";
97             otherName = 1;
98  
99             // The var data type is defined when compiled and then can't change
100             var anotherName = "Tom";
101             // ERROR : anotherName = 2;
102             Console.WriteLine("Hello " + anotherName);
103  
http://www.newthinktank.com/2015/07/learn-c-one-video/ 3/18
12/11/2018 Learn C# in One Video
104             // How to get the type and how to format strings
105             Console.WriteLine("anotherName is a {0}", anotherName.GetTypeCode());
106  
107             // ---------- MATH ----------
108  
109             Console.WriteLine("5 + 3 = " + (5 + 3));
110             Console.WriteLine("5 - 3 = " + (5 - 3));
111             Console.WriteLine("5 * 3 = " + (5 * 3));
112             Console.WriteLine("5 / 3 = " + (5 / 3));
113             Console.WriteLine("5.2 % 3 = " + (5.2 % 3));
114  
115             int i = 0;
116  
117             Console.WriteLine("i++ = " + (i++));
118             Console.WriteLine("++i = " + (++i));
119             Console.WriteLine("i-- = " + (i--));
120             Console.WriteLine("--i = " + (--i));
121  
122             Console.WriteLine("i += 3 " + (i += 3));
123             Console.WriteLine("i -= 2 " + (i -= 2));
124             Console.WriteLine("i *= 2 " + (i *= 2));
125             Console.WriteLine("i /= 2 " + (i /= 2));
126             Console.WriteLine("i %= 2 " + (i %= 2));
127  
128             // Casting : If no magnitude is lost casting happens automatically, but otherwise it
129             // like this
130  
131             double pi = 3.14;
132             int intPi = (int)pi; // put the data type to convert to between braces
133  
134             // Math Functions
135             // Acos, Asin, Atan, Atan2, Cos, Cosh, Exp, Log, Sin, Sinh, Tan, Tanh
136             double number1 = 10.5;
137             double number2 = 15;
138  
139             Console.WriteLine("Math.Abs(number1) " + (Math.Abs(number1)));
140             Console.WriteLine("Math.Ceiling(number1) " + (Math.Ceiling(number1)));
141             Console.WriteLine("Math.Floor(number1) " + (Math.Floor(number1)));
142             Console.WriteLine("Math.Max(number1, number2) " + (Math.Max(number1, number2)));
143             Console.WriteLine("Math.Min(number1, number2) " + (Math.Min(number1, number2)));
144             Console.WriteLine("Math.Pow(number1, 2) " + (Math.Pow(number1, 2)));
145             Console.WriteLine("Math.Round(number1) " + (Math.Round(number1)));
146             Console.WriteLine("Math.Sqrt(number1) " + (Math.Sqrt(number1)));
147  
148             // Random Numbers
149             Random rand = new Random();
150             Console.WriteLine("Random Number Between 1 and 10 " + (rand.Next(1,11)));
151  
152             // ---------- CONDITIONALS ----------
153  
154             // Relational Operators : > < >= <= == !=
155             // Logical Operators : && || ^ !
156  
157             // If Statement
158             int age = 17;
159  
160             if ((age >= 5) && (age <= 7)) {
161                 Console.WriteLine("Go to elementary school");
162             }
163             else if ((age > 7) && (age < 13)) {
164                 Console.WriteLine("Go to middle school");
165             }
166             else {
167                 Console.WriteLine("Go to high school");
168             }
http://www.newthinktank.com/2015/07/learn-c-one-video/ 4/18
12/11/2018 Learn C# in One Video
169  
170             if ((age < 14) || (age > 67)) {
171                 Console.WriteLine("You shouldn't work");
172             }
173  
174             Console.WriteLine("! true = " + (! true));
175  
176             // Ternary Operator
177  
178             bool canDrive = age >= 16 ? true : false;
179  
180             // Switch is used when you have limited options
181             // Fall through isn't allowed with C# unless there are no statements between cases
182             // You can't check multiple values at once
183  
184             switch (age)
185             {
186                 case 0:
187                     Console.WriteLine("Infant");
188                     break;
189                 case 1:
190                 case 2:
191                     Console.WriteLine("Toddler");
192  
193             // Goto can be used to jump to a label elsewhere in the code
194                     goto Cute;
195                 default:
196                     Console.WriteLine("Child");
197                     break;
198             }
199  
200             // Lable we can jump to with Goto
201             Cute:
202             Console.WriteLine("Toddlers are cute");
203  
204             // ---------- LOOPING ----------
205  
206             int i = 0;
207  
208             while (i < 10)
209             {
210                 // If i = 7 then skip the rest of the code and start with i = 8
211                 if (i == 7)
212                 {
213                     i++;
214                     continue;
215                 }
216  
217                 // Jump completely out of the loop if i = 9
218                 if (i == 9)
219                 {
220                     break;
221                 }
222  
223                 // You can't convert an int into a bool : Print out only odds
224                 if ((i % 2) > 0)
225                 {
226                     Console.WriteLine(i);
227                 }
228                 i++;
229             }
230  
231             // The do while loop will go through the loop at least once
232             string guess;
233             do
http://www.newthinktank.com/2015/07/learn-c-one-video/ 5/18
12/11/2018 Learn C# in One Video
234             {
235                 Console.WriteLine("Guess a Number ");
236                 guess = Console.ReadLine();
237             } while (! guess.Equals("15")); // How to check String equality
238  
239             // Puts all changes to the iterator in one place
240             for(int j = 0; j < 10; j++)
241             {
242                 if ((j % 2) > 0)
243                 {
244                     Console.WriteLine(j);
245                 }
246             }
247  
248             // foreach cycles through every item in an array or collection
249             string randStr = "Here are some random characters";
250  
251             foreach( char c in randStr)
252             {
253                 Console.WriteLine(c);
254             }
255  
256             // ---------- STRINGS ----------
257  
258             // Escape Sequences : \' \" \\ \b \n \t
259  
260             string sampString = "A bunch of random words";
261  
262             // Check if empty
263             Console.WriteLine("Is empty " + String.IsNullOrEmpty(sampString));
264             Console.WriteLine("Is empty " + String.IsNullOrWhiteSpace(sampString));
265             Console.WriteLine("String Length " + sampString.Length);
266  
267             // Find a string index (Starts with 0)
268             Console.WriteLine("Index of bunch " + sampString.IndexOf("bunch"));
269  
270             // Get a substring
271             Console.WriteLine("2nd Word " + sampString.Substring(2, 6));
272  
273             string sampString2 = "More random words";
274  
275             // Are strings equal
276             Console.WriteLine("Strings equal " + sampString.Equals(sampString2));
277  
278             // Compare strings
279             Console.WriteLine("Starts with A bunch " + sampString.StartsWith("A bunch"));
280             Console.WriteLine("Ends with words " + sampString.EndsWith("words"));
281  
282             // Trim white space at beginning and end or (TrimEnd / TrimStart)
283             sampString = sampString.Trim();
284  
285             // Replace words or characters
286             sampString = sampString.Replace("words", "characters");
287             Console.WriteLine(sampString);
288  
289             // Remove starting at a defined index up to the second index
290             sampString = sampString.Remove(0,2);
291             Console.WriteLine(sampString);
292  
293             // Join values in array and save to string
294             string[] names = new string[3] { "Matt", "Joe", "Paul" };
295             Console.WriteLine("Name List " + String.Join(", ", names));
296  
297             // Formatting : Currency, Decimal Places, Before Decimals, Thousands Separator
298             string fmtStr = String.Format("{0:c} {1:00.00} {2:#.00} {3:0,0}", 1.56, 15.567, .56
http://www.newthinktank.com/2015/07/learn-c-one-video/ 6/18
12/11/2018 Learn C# in One Video

299  
300             Console.WriteLine(fmtStr.ToString());
301  
302             // ---------- STRINGBUILDER ----------
303             // Each time you create a string you actually create another string in memory
304             // StringBuilders are used when you want to be able to edit a string without creati
305  
306             StringBuilder sb = new StringBuilder();
307  
308             // Append a string to the StringBuilder (AppendLine also adds a newline at the end)
309             sb.Append("This is the first sentence.");
310  
311             // Append a formatted string
312             sb.AppendFormat("My name is {0} and I live in {1}", "Derek", "Pennsylvania");
313  
314             // Clear the StringBuilder
315             // sb.Clear();
316  
317             // Replaces every instance of the first with the second
318             sb.Replace("a", "e");
319  
320             // Remove characters starting at the index and then up to the defined index
321             sb.Remove(5, 7);
322  
323             // Out put everything
324             Console.WriteLine(sb.ToString());
325  
326             // ---------- ARRAYS ----------
327             // Declare an array
328             int[] randNumArray;
329  
330             // Declare the number of items an array can contain
331             int[] randArray = new int[5];
332  
333             // Declare and initialize an array
334             int[] randArray2 = { 1, 2, 3, 4, 5 };
335  
336             // Get array length
337             Console.WriteLine("Array Length " + randArray2.Length);
338  
339             // Get item at index
340             Console.WriteLine("Item 0 " + randArray2[0]);
341  
342             // Cycle through array
343             for (int i = 0; i < randArray2.Length; i++)
344             {
345                 Console.WriteLine("{0} : {1}", i, randArray2[i]);
346             }
347  
348             // Cycle with foreach
349             foreach (int num in randArray2)
350             {
351                 Console.WriteLine(num);
352             }
353  
354             // Get the index of an item or -1
355             Console.WriteLine("Where is 1 " + Array.IndexOf(randArray2, 1));
356  
357             string[] names = { "Tom", "Paul", "Sally" };
358  
359             // Join an array into a string
360             string nameStr = string.Join(", ", names);
361             Console.WriteLine(nameStr);
362  
363             // Split a string into an array
http://www.newthinktank.com/2015/07/learn-c-one-video/ 7/18
12/11/2018 Learn C# in One Video

364             string[] nameArray = nameStr.Split(',');


365  
366             // Create a multidimensional array
367             int[,] multArray = new int[5, 3];
368  
369             // Create and initialize a multidimensional array
370             int[,] multArray2 = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
371  
372             // Cycle through multidimensional array
373             foreach(int num in multArray2)
374             {
375                 Console.WriteLine(num);
376             }
377  
378             // Cycle and have access to indexes
379             for (int x = 0; x < multArray2.GetLength(0); x += 1)
380             {
381                 for (int y = 0; y < multArray2.GetLength(1); y += 1)
382                 {
383                     Console.WriteLine("{0} | {1} : {2}", x, y, multArray2[x, y]);
384                 }
385             }
386  
387             // ---------- LISTS ----------
388             // A list unlike an array automatically resizes
389  
390             // Create a list and add values
391             List<int> numList = new List<int>();
392             numList.Add(5);
393             numList.Add(15);
394             numList.Add(25);
395  
396             // Add an array to a list
397             int[] randArray = { 1, 2, 3, 4, 5 };
398             numList.AddRange(randArray);
399  
400             // Clear a list
401             // numList.Clear();
402  
403             // Copy an array into a List
404             List<int> numList2 = new List<int>(randArray);
405  
406             // Create a List with array
407             List<int> numList3 = new List<int>(new int[] { 1, 2, 3, 4 });
408  
409             // Insert in a specific index
410             numList.Insert(1, 10);
411  
412             // Remove a specific value
413             numList.Remove(5);
414  
415             // Remove at an index
416             numList.RemoveAt(2);
417  
418             // Cycle through a List with foreach or
419             for (int i = 0; i < numList.Count; i++)
420             {
421                 Console.WriteLine(numList[i]);
422             }
423  
424             // Return the index for a value or -1
425             Console.WriteLine("4 is in index " + numList3.IndexOf(4));
426  
427             // Does the List contain a value
428             Console.WriteLine("5 in list " + numList3.Contains(5));
http://www.newthinktank.com/2015/07/learn-c-one-video/ 8/18
12/11/2018 Learn C# in One Video

429  
430             // Search for a value in a string List
431             List<string> strList = new List<string>(new string[] { "Tom","Paul" });
432             Console.WriteLine("Tom in list " + strList.Contains("tom", StringComparer.OrdinalIgn
433  
434             // Sort the List
435             strList.Sort();
436  
437             // ---------- EXCEPTION HANDLING ----------
438             // All the exceptions
439             // msdn.microsoft.com/en-us/library/system.systemexception.aspx#inheritanceContinued
440  
441                 try
442                 {
443                     Console.Write("Divide 10 by ");
444                     int num = int.Parse(Console.ReadLine());
445                     Console.WriteLine("10 / {0} =  {1}", num, (10/num));
446  
447                 }
448  
449                 // Specifically catches the divide by zero exception
450                 catch (DivideByZeroException ex)
451                 {
452                     Console.WriteLine("Can't divide by zero");
453  
454                     // Get additonal info on the exception
455                     Console.WriteLine(ex.GetType().Name);
456                     Console.WriteLine(ex.Message);
457  
458                     // Throw the exception to the next inline
459                     // throw ex;
460  
461                     // Throw a specific exception
462                     throw new InvalidOperationException("Operation Failed", ex);
463                 }
464  
465                 // Catches any other exception
466                 catch (Exception ex)
467                 {
468                     Console.WriteLine("An error occurred");
469                     Console.WriteLine(ex.GetType().Name);
470                     Console.WriteLine(ex.Message);
471                 }
472  
473             // ---------- CLASSES & OBJECTS ----------
474  
475             Animal bulldog = new Animal(13, 50, "Spot", "Woof");
476  
477             Console.WriteLine("{0} says {1}", bulldog.name, bulldog.sound);
478  
479             // Console.WriteLine("No. of Animals " + Animal.getNumOfAnimals());
480  
481             // ---------- ENUMS ----------
482  
483             Temperature micTemp = Temperature.Low;
484             Console.Write("What Temp : ");
485  
486             Console.ReadLine();
487  
488             switch (micTemp)
489             {
490                 case Temperature.Freeze:
491                     Console.WriteLine("Temp on Freezing");
492                     break;
493  
http://www.newthinktank.com/2015/07/learn-c-one-video/ 9/18
12/11/2018 Learn C# in One Video

494                 case Temperature.Low:


495                     Console.WriteLine("Temp on Low");
496                     break;
497  
498                 case Temperature.Warm:
499                     Console.WriteLine("Temp on Warm");
500                     break;
501  
502                 case Temperature.Boil:
503                     Console.WriteLine("Temp on Boil");
504                     break;
505             }
506  
507             // ---------- STRUCTS ----------
508             Customers bob = new Customers();
509  
510             bob.createCust("Bob", 15.50, 12345);
511  
512             bob.showCust();
513  
514             // ---------- ANONYMOUS METHODS ----------
515             // An anonymous method has no name and its return type is defined by the return used
516  
517             GetSum sum = delegate (double num1, double num2) {
518                 return num1 + num2;
519             };
520  
521             Console.WriteLine("5 + 10 = " + sum(5, 10));
522  
523             // ---------- LAMBDA EXPRESSIONS ----------
524             // A lambda expression is used to act as an anonymous function or expression tree
525  
526             // You can assign the lambda expression to a function instance
527             Func<int, int, int> getSum = (x, y) => x + y;
528             Console.WriteLine("5 + 3 = " + getSum.Invoke(5, 3));
529  
530             // Get odd numbers from a list
531             List<int> numList = new List<int> { 5, 10, 15, 20, 25 };
532  
533             // With an Expression Lambda the input goes in the left (n) and the statements go o
534             List<int> oddNums = numList.Where(n => n % 2 == 1).ToList();
535  
536             foreach (int num in oddNums) {
537                 Console.Write(num + ", ");
538             }
539  
540             // ---------- FILE I/O ----------
541             // The StreamReader and StreamWriter allows you to create text files while reading a
542             // writing to them
543  
544             string[] custs = new string[] { "Tom", "Paul", "Greg" };
545  
546             using (StreamWriter sw = new StreamWriter("custs.txt"))
547             {
548                 foreach(string cust in custs)
549                 {
550                     sw.WriteLine(cust);
551                 }
552             }
553  
554             string custName = "";
555             using (StreamReader sr = new StreamReader("custs.txt"))
556             {
557                 while ((custName = sr.ReadLine()) != null)
558                 {
http://www.newthinktank.com/2015/07/learn-c-one-video/ 10/18
12/11/2018 Learn C# in One Video

559                     Console.WriteLine(custName);
560                 }
561             }
562  
563             Console.Write("Hit Enter to Exit");
564             string exitApp = Console.ReadLine();
565  
566         }
567     }
568 }

1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
7  
8 namespace ConsoleApplication2
9 {
10     class Animal
11     {
12  
13         // public : Access is not limited
14         // protected : Access is limited to the class methods and subclasses
15         // private : Access is limited to only this classes methods
16         public double height { get; set; }
17         public double weight { get; set; }
18         public string sound { get; set; }
19  
20         // We can either have C# create the getters and setters or create them ourselves to ver
21         public string name;
22         public string Name
23         {
24             get { return name; }
25             set { name = value; }
26         }
27  
28         // Every object has a default constructor that receives no attributes
29         // The constructor initializes every object created
30         // this is used to refer to this objects specific fields since we don't know the object
31  
32         // The default constructor isn't created if you create any other constructor
33         public Animal()
34         {
35             this.height = 0;
36             this.weight = 0;
37             this.name = "No Name";
38             this.sound = "No Sound";
39  
40             numOfAnimals++;
41         }
42  
43         // You can create custom constructors as well
44         public Animal(double height, double weight, string name, string sound)
45         {
46             this.height = height;
47             this.weight = weight;
48             this.name = name;
49             this.sound = sound;
50  
51             numOfAnimals++;
52         }
53  
54         // A static fields value is shared by every object of the Animal class
http://www.newthinktank.com/2015/07/learn-c-one-video/ 11/18
12/11/2018 Learn C# in One Video
55         // We declare thinsg static when it doesn't make sense for our object to either have the
56         // the capability to do something (Animals can't count)
57         static int numOfAnimals = 0;
58  
59         // A static method cannot access non-static class members
60         public static int getNumOfAnimals()
61         {
62             return numOfAnimals;
63         }
64  
65         // Declare a method
66         public string toString()
67         {
68             return String.Format("{0} is {1} inches tall, weighs {2} lbs and likes to say {3}",
69         }
70  
71         // Overloading methods works if you have methods with different attribute data types
72         // You can give attributes default values
73         public int getSum(int num1 = 1, int num2 = 1)
74         {
75             return num1 + num2;
76         }
77  
78         public double getSum(double num1, double num2)
79         {
80             return num1 + num2;
81         }
82  
83         static void Main(string[] args)
84         {
85             // Create an Animal object and call the constructor
86             Animal spot = new Animal(15, 10, "Spot", "Woof");
87  
88             // Get object values with the dot operator
89             Console.WriteLine("{0} says {1}", spot.name, spot.sound);
90  
91             // Calling a static method
92             Console.WriteLine("Number of Animals " + Animal.getNumOfAnimals());
93  
94             // Calling an object method
95             Console.WriteLine(spot.toString());
96  
97             Console.WriteLine("3 + 4 = " + spot.getSum(3, 4));
98  
99             // You can assign attributes by name
100             Console.WriteLine("3.4 + 4.5 = " + spot.getSum(num2: 3.4, num1: 4.5));
101  
102             // You can create objects with an object initializer
103             Animal grover = new Animal
104             {
105                 name = "Grover",
106                 height = 16,
107                 weight = 18,
108                 sound = "Grrr"
109             };
110  
111             Console.WriteLine(grover.toString());
112  
113             // Create a subclass Dog object
114             Dog spike = new Dog();
115  
116             Console.WriteLine(spike.toString());
117  
118             spike = new Dog(20, 15, "Spike", "Grrr Woof", "Chicken");
119  
http://www.newthinktank.com/2015/07/learn-c-one-video/ 12/18
12/11/2018 Learn C# in One Video
120             Console.WriteLine(spike.toString());
121  
122             // One way to implement polymorphism is through an abstract class
123             Shape rect = new Rectangle(5, 5);
124             Shape tri = new Triangle(5, 5);
125             Console.WriteLine("Rect Area " + rect.area());
126             Console.WriteLine("Trit Area " + tri.area());
127  
128             // Using the overloaded + on 2 Rectangles
129             Rectangle combRect = new Rectangle(5, 5) + new Rectangle(5, 5);
130  
131             Console.WriteLine("combRect Area = " + combRect.area());
132  
133             // ---------- GENERICS ----------
134             // With Generics you don't have to specify the data type of an element in a class o
135             KeyValue<string, string> superman = new KeyValue<string, string>("","");
136             superman.key = "Superman";
137             superman.value = "Clark Kent";
138             superman.showData();
139  
140             // Now use completely different types
141             KeyValue<int, string> samsungTV = new KeyValue<int, string>(0, "");
142             samsungTV.key = 123456;
143             samsungTV.value = "a 50in Samsung TV";
144             samsungTV.showData();
145  
146             Console.Write("Hit Enter to Exit");
147             string exitApp = Console.ReadLine();
148  
149         }
150     }
151  
152     class Dog : Animal
153     {
154         public string favFood { get; set; }
155  
156         // Set the favFood default and then call the Animal super class constructor
157         public Dog() : base()
158         {
159             this.favFood = "No Favorite Food";
160         }
161  
162         public Dog(double height, double weight, string name, string sound, string favFood) :
163             base(height, weight, name, sound)
164         {
165             this.favFood = favFood;
166         }
167  
168         // Override methods with the keyword new
169         new public string toString()
170         {
171             return String.Format("{0} is {1} inches tall, weighs {2} lbs, likes to say {3} and e
172         }
173  
174     }
175  
176     // Abstract classes define methods that must be defined by derived classes
177     // You can only inherit one abstract class per class
178     // You can't instantiate an abstract class
179     abstract class Shape
180     {
181         public abstract double area();
182  
183         // An abstract class can contain complete or default code for methods
184         public void sayHi()
http://www.newthinktank.com/2015/07/learn-c-one-video/ 13/18
12/11/2018 Learn C# in One Video
185         {
186             Console.WriteLine("Hello");
187         }
188     }
189  
190     // A class can have many interfaces
191     // An interface can't have concrete code
192     public interface ShapeItem
193     {
194         double area();
195     }
196  
197     class Rectangle : Shape
198     {
199         private double length;
200         private double width;
201  
202         public Rectangle( double num1, double num2)
203         {
204             length = num1;
205             width = num2;
206         }
207  
208         public override double area()
209         {
210             return length * width;
211         }
212  
213         // You can redefine many built in operators so that you can define what happens when you
214         // add to Rectangles
215         public static Rectangle operator+ (Rectangle rect1, Rectangle rect2)
216         {
217             double rectLength = rect1.length + rect2.length;
218             double rectWidth = rect1.width + rect2.width;
219  
220             return new Rectangle(rectLength, rectWidth);
221  
222         }
223  
224     }
225  
226     class Triangle : Shape
227     {
228         private double theBase;
229         private double height;
230  
231         public Triangle(double num1, double num2)
232         {
233             theBase = num1;
234             height = num2;
235         }
236  
237         public override double area()
238         {
239             return .5 * (theBase * height);
240         }
241     }
242  
243     // ---------- GENERIC CLASS ----------
244  
245     class KeyValue<TKey, TValue>
246     {
247         public TKey key { get; set; }
248         public TValue value { get; set; }
249  
http://www.newthinktank.com/2015/07/learn-c-one-video/ 14/18
12/11/2018 Learn C# in One Video
250         public KeyValue(TKey k, TValue v)
251         {
252             key = k;
253             value = v;
254         }
255  
256         public void showData()
257         {
258             Console.WriteLine("{0} is {1}", this.key, this.value);
259         }
260  
261     }
262  
263 }

Leave a Reply
Your email address will not be published.

Comment

Name

Email

Website

Submit Comment

Search
Search
Social Networks

Facebook
YouTube
Twitter
LinkedIn

Buy me a Cup of Coffee


"Donations help me to keep the site running. One dollar is greatly appreciated." - (Pay Pal Secured)

My Facebook Page
Like Share 6K people like this. Sign Up to
see what your friends like.
Archives

http://www.newthinktank.com/2015/07/learn-c-one-video/ 15/18
12/11/2018 Learn C# in One Video

October 2018
September 2018
August 2018
July 2018
June 2018
May 2018
April 2018
March 2018
February 2018
January 2018
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
February 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
http://www.newthinktank.com/2015/07/learn-c-one-video/ 16/18
12/11/2018 Learn C# in One Video

April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
October 2013
September 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
May 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009

http://www.newthinktank.com/2015/07/learn-c-one-video/ 17/18
12/11/2018 Learn C# in One Video

Powered by WordPress | Designed by Elegant Themes


About the Author Google+

http://www.newthinktank.com/2015/07/learn-c-one-video/ 18/18

Das könnte Ihnen auch gefallen