You are on page 1of 21

Program.

cs
C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnimalNS;
/*
Multiline Comment
*/
// Delegates are used to pass methods as arguments to other methods
// A delegate can represent a method with a similar return type and attribute list
delegate double GetSum(double num1, double num2);
// ---------- ENUMS ---------// Enums are unique types with symbolic names and associated values
public enum Temperature
{
Freeze,
Low,
Warm,
Boil
}
// ---------- STRUCT ---------// A struct is a custom type that holds data made up from different data types
struct Customers
{
private string name;
private double balance;
private int id;
public void createCust(string n, double b, int i)
{
name = n;
balance = b;
id = i;
}
public void showCust()
{
Console.WriteLine("Name : " + name);
Console.WriteLine("Balance : " + balance);
Console.WriteLine("ID : " + id);
}
}
// Give our code a custom namespace
namespace ConsoleApplication1
{
class Program
{

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
87 precision
88
float maxFloat = float.MaxValue;
89
90
// A float is a 32 bit number with a maxValue of 1.797693134E+308 with 15 decimals of
91 precision
92
double maxDouble = double.MaxValue;
93
94
// You can combine strings with other values with +
95
Console.WriteLine("Max Int : " + maxDouble);
96
97
// The dynamic data type is defined at run time
98
dynamic otherName = "Paul";
99
otherName = 1;
10
0
// The var data type is defined when compiled and then can't change
10
var anotherName = "Tom";
1
// ERROR : anotherName = 2;
10
Console.WriteLine("Hello " + anotherName);
2
10
// How to get the type and how to format strings
3
Console.WriteLine("anotherName is a {0}", anotherName.GetTypeCode());
10
4
// ---------- MATH ---------10
5
Console.WriteLine("5 + 3 = " + (5 + 3));
10
Console.WriteLine("5 - 3 = " + (5 - 3));
6
Console.WriteLine("5 * 3 = " + (5 * 3));
10
Console.WriteLine("5 / 3 = " + (5 / 3));
7
Console.WriteLine("5.2 % 3 = " + (5.2 % 3));
10
8
int i = 0;
10
9
Console.WriteLine("i++ = " + (i++));
11
Console.WriteLine("++i = " + (++i));
0
Console.WriteLine("i-- = " + (i--));

11
Console.WriteLine("--i = " + (--i));
1
11
Console.WriteLine("i += 3 " + (i += 3));
2
Console.WriteLine("i -= 2 " + (i -= 2));
11
Console.WriteLine("i *= 2 " + (i *= 2));
3
Console.WriteLine("i /= 2 " + (i /= 2));
11
Console.WriteLine("i %= 2 " + (i %= 2));
4
11
// Casting : If no magnitude is lost casting happens automatically, but otherwise it must
5 be done
11
// like this
6
11
double pi = 3.14;
7
int intPi = (int)pi; // put the data type to convert to between braces
11
8
// Math Functions
11
// Acos, Asin, Atan, Atan2, Cos, Cosh, Exp, Log, Sin, Sinh, Tan, Tanh
9
double number1 = 10.5;
12
double number2 = 15;
0
12
Console.WriteLine("Math.Abs(number1) " + (Math.Abs(number1)));
1
Console.WriteLine("Math.Ceiling(number1) " + (Math.Ceiling(number1)));
12
Console.WriteLine("Math.Floor(number1) " + (Math.Floor(number1)));
2
Console.WriteLine("Math.Max(number1, number2) " + (Math.Max(number1,
12 number2)));
3
Console.WriteLine("Math.Min(number1, number2) " + (Math.Min(number1, number2)));
12
Console.WriteLine("Math.Pow(number1, 2) " + (Math.Pow(number1, 2)));
4
Console.WriteLine("Math.Round(number1) " + (Math.Round(number1)));
12
Console.WriteLine("Math.Sqrt(number1) " + (Math.Sqrt(number1)));
5
12
// Random Numbers
6
Random rand = new Random();
12
Console.WriteLine("Random Number Between 1 and 10 " + (rand.Next(1,11)));
7
12
// ---------- CONDITIONALS ---------8
12
// Relational Operators : > < >= <= == !=
9
// Logical Operators : && || ^ !
13
0
// If Statement
13
int age = 17;
1
13
if ((age >= 5) && (age <= 7)) {
2
Console.WriteLine("Go to elementary school");
13
}
3
else if ((age > 7) && (age < 13)) {
13
Console.WriteLine("Go to middle school");
4
}
13
else {
5
Console.WriteLine("Go to high school");
13
}
6
13
if ((age < 14) || (age > 67)) {
7
Console.WriteLine("You shouldn't work");
13
}
8
13
Console.WriteLine("! true = " + (! true));
9
14
// Ternary Operator
0
14
bool canDrive = age >= 16 ? true : false;
1
14
// Switch is used when you have limited options
2
// Fall through isn't allowed with C# unless there are no statements between cases
14
// You can't check multiple values at once
3

14
4
14
5
14
6
14
7
14
8
14
9
15
0
15
1
15
2
15
3
15
4
15
5
15
6
15
7
15
8
15
9
16
0
16
1
16
2
16
3
16
4
16
5
16
6
16
7
16
8
16
9
17
0
17
1
17
2
17
3
17
4
17
5
17
6

switch (age)
{
case 0:
Console.WriteLine("Infant");
break;
case 1:
case 2:
Console.WriteLine("Toddler");
// Goto can be used to jump to a label elsewhere in the code
goto Cute;
default:
Console.WriteLine("Child");
break;
}
// Lable we can jump to with Goto
Cute:
Console.WriteLine("Toddlers are cute");
// ---------- LOOPING ---------int i = 0;
while (i < 10)
{
// If i = 7 then skip the rest of the code and start with i = 8
if (i == 7)
{
i++;
continue;
}
// Jump completely out of the loop if i = 9
if (i == 9)
{
break;
}
// You can't convert an int into a bool : Print out only odds
if ((i % 2) > 0)
{
Console.WriteLine(i);
}
i++;
}
// The do while loop will go through the loop at least once
string guess;
do
{
Console.WriteLine("Guess a Number ");
guess = Console.ReadLine();
} while (! guess.Equals("15")); // How to check String equality
// Puts all changes to the iterator in one place
for(int j = 0; j < 10; j++)
{
if ((j % 2) > 0)
{
Console.WriteLine(j);
}
}
// foreach cycles through every item in an array or collection
string randStr = "Here are some random characters";

17
7
foreach( char c in randStr)
17
{
8
Console.WriteLine(c);
17
}
9
18
// ---------- STRINGS ---------0
18
// Escape Sequences : \' \" \\ \b \n \t
1
18
string sampString = "A bunch of random words";
2
18
// Check if empty
3
Console.WriteLine("Is empty " + String.IsNullOrEmpty(sampString));
18
Console.WriteLine("Is empty " + String.IsNullOrWhiteSpace(sampString));
4
Console.WriteLine("String Length " + sampString.Length);
18
5
// Find a string index (Starts with 0)
18
Console.WriteLine("Index of bunch " + sampString.IndexOf("bunch"));
6
18
// Get a substring
7
Console.WriteLine("2nd Word " + sampString.Substring(2, 6));
18
8
string sampString2 = "More random words";
18
9
// Are strings equal
19
Console.WriteLine("Strings equal " + sampString.Equals(sampString2));
0
19
// Compare strings
1
Console.WriteLine("Starts with A bunch " + sampString.StartsWith("A bunch"));
19
Console.WriteLine("Ends with words " + sampString.EndsWith("words"));
2
19
// Trim white space at beginning and end or (TrimEnd / TrimStart)
3
sampString = sampString.Trim();
19
4
// Replace words or characters
19
sampString = sampString.Replace("words", "characters");
5
Console.WriteLine(sampString);
19
6
// Remove starting at a defined index up to the second index
19
sampString = sampString.Remove(0,2);
7
Console.WriteLine(sampString);
19
8
// Join values in array and save to string
19
string[] names = new string[3] { "Matt", "Joe", "Paul" };
9
Console.WriteLine("Name List " + String.Join(", ", names));
20
0
// Formatting : Currency, Decimal Places, Before Decimals, Thousands Separator
20
string fmtStr = String.Format("{0:c} {1:00.00} {2:#.00} {3:0,0}", 1.56, 15.567, .56,
1 1000);
20
2
Console.WriteLine(fmtStr.ToString());
20
3
// ---------- STRINGBUILDER ---------20
// Each time you create a string you actually create another string in memory
4
// StringBuilders are used when you want to be able to edit a string without creating
20 new ones
5
20
StringBuilder sb = new StringBuilder();
6
20
// Append a string to the StringBuilder (AppendLine also adds a newline at the end)
7
sb.Append("This is the first sentence.");
20
8
// Append a formatted string
20
sb.AppendFormat("My name is {0} and I live in {1}", "Derek", "Pennsylvania");
9

21
0
21
1
21
2
21
3
21
4
21
5
21
6
21
7
21
8
21
9
22
0
22
1
22
2
22
3
22
4
22
5
22
6
22
7
22
8
22
9
23
0
23
1
23
2
23
3
23
4
23
5
23
6
23
7
23
8
23
9
24
0
24
1
24
2

// Clear the StringBuilder


// sb.Clear();
// Replaces every instance of the first with the second
sb.Replace("a", "e");
// Remove characters starting at the index and then up to the defined index
sb.Remove(5, 7);
// Out put everything
Console.WriteLine(sb.ToString());
// ---------- ARRAYS ---------// Declare an array
int[] randNumArray;
// Declare the number of items an array can contain
int[] randArray = new int[5];
// Declare and initialize an array
int[] randArray2 = { 1, 2, 3, 4, 5 };
// Get array length
Console.WriteLine("Array Length " + randArray2.Length);
// Get item at index
Console.WriteLine("Item 0 " + randArray2[0]);
// Cycle through array
for (int i = 0; i < randArray2.Length; i++)
{
Console.WriteLine("{0} : {1}", i, randArray2[i]);
}
// Cycle with foreach
foreach (int num in randArray2)
{
Console.WriteLine(num);
}
// Get the index of an item or -1
Console.WriteLine("Where is 1 " + Array.IndexOf(randArray2, 1));
string[] names = { "Tom", "Paul", "Sally" };
// Join an array into a string
string nameStr = string.Join(", ", names);
Console.WriteLine(nameStr);
// Split a string into an array
string[] nameArray = nameStr.Split(',');
// Create a multidimensional array
int[,] multArray = new int[5, 3];
// Create and initialize a multidimensional array
int[,] multArray2 = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
// Cycle through multidimensional array
foreach(int num in multArray2)
{
Console.WriteLine(num);
}
// Cycle and have access to indexes
for (int x = 0; x < multArray2.GetLength(0); x += 1)

24
{
3
for (int y = 0; y < multArray2.GetLength(1); y += 1)
24
{
4
Console.WriteLine("{0} | {1} : {2}", x, y, multArray2[x, y]);
24
}
5
}
24
6
// ---------- LISTS ---------24
// A list unlike an array automatically resizes
7
24
// Create a list and add values
8
List<int> numList = new List<int>();
24
numList.Add(5);
9
numList.Add(15);
25
numList.Add(25);
0
25
// Add an array to a list
1
int[] randArray = { 1, 2, 3, 4, 5 };
25
numList.AddRange(randArray);
2
25
// Clear a list
3
// numList.Clear();
25
4
// Copy an array into a List
25
List<int> numList2 = new List<int>(randArray);
5
25
// Create a List with array
6
List<int> numList3 = new List<int>(new int[] { 1, 2, 3, 4 });
25
7
// Insert in a specific index
25
numList.Insert(1, 10);
8
25
// Remove a specific value
9
numList.Remove(5);
26
0
// Remove at an index
26
numList.RemoveAt(2);
1
26
// Cycle through a List with foreach or
2
for (int i = 0; i < numList.Count; i++)
26
{
3
Console.WriteLine(numList[i]);
26
}
4
26
// Return the index for a value or -1
5
Console.WriteLine("4 is in index " + numList3.IndexOf(4));
26
6
// Does the List contain a value
26
Console.WriteLine("5 in list " + numList3.Contains(5));
7
26
// Search for a value in a string List
8
List<string> strList = new List<string>(new string[] { "Tom","Paul" });
26
Console.WriteLine("Tom in list " + strList.Contains("tom",
9 StringComparer.OrdinalIgnoreCase));
27
0
// Sort the List
27
strList.Sort();
1
27
// ---------- EXCEPTION HANDLING ---------2
// All the exceptions
27
// msdn.microsoft.com/en3 us/library/system.systemexception.aspx#inheritanceContinued
27
4
try
27
{
5
Console.Write("Divide 10 by ");

27
6
27
7
27
8
27
9
28
0
28
1
28
2
28
3
28
4
28
5
28
6
28
7
28
8
28
9
29
0
29
1
29
2
29
3
29
4
29
5
29
6
29
7
29
8
29
9
30
0
30
1
30
2
30
3
30
4
30
5
30
6
30
7
30
8

int num = int.Parse(Console.ReadLine());


Console.WriteLine("10 / {0} = {1}", num, (10/num));
}
// Specifically catches the divide by zero exception
catch (DivideByZeroException ex)
{
Console.WriteLine("Can't divide by zero");
// Get additonal info on the exception
Console.WriteLine(ex.GetType().Name);
Console.WriteLine(ex.Message);
// Throw the exception to the next inline
// throw ex;
// Throw a specific exception
throw new InvalidOperationException("Operation Failed", ex);
}
// Catches any other exception
catch (Exception ex)
{
Console.WriteLine("An error occurred");
Console.WriteLine(ex.GetType().Name);
Console.WriteLine(ex.Message);
}
// ---------- CLASSES & OBJECTS ---------Animal bulldog = new Animal(13, 50, "Spot", "Woof");
Console.WriteLine("{0} says {1}", bulldog.name, bulldog.sound);
// Console.WriteLine("No. of Animals " + Animal.getNumOfAnimals());
// ---------- ENUMS ---------Temperature micTemp = Temperature.Low;
Console.Write("What Temp : ");
Console.ReadLine();
switch (micTemp)
{
case Temperature.Freeze:
Console.WriteLine("Temp on Freezing");
break;
case Temperature.Low:
Console.WriteLine("Temp on Low");
break;
case Temperature.Warm:
Console.WriteLine("Temp on Warm");
break;
case Temperature.Boil:
Console.WriteLine("Temp on Boil");
break;
}
// ---------- STRUCTS ---------Customers bob = new Customers();

30
bob.createCust("Bob", 15.50, 12345);
9
31
bob.showCust();
0
31
// ---------- ANONYMOUS METHODS ---------1
// An anonymous method has no name and its return type is defined by the return used
31 in the method
2
31
GetSum sum = delegate (double num1, double num2) {
3
return num1 + num2;
31
};
4
31
Console.WriteLine("5 + 10 = " + sum(5, 10));
5
31
// ---------- LAMBDA EXPRESSIONS ---------6
// A lambda expression is used to act as an anonymous function or expression tree
31
7
// You can assign the lambda expression to a function instance
31
Func<int, int, int> getSum = (x, y) => x + y;
8
Console.WriteLine("5 + 3 = " + getSum.Invoke(5, 3));
31
9
// Get odd numbers from a list
32
List<int> numList = new List<int> { 5, 10, 15, 20, 25 };
0
32
// With an Expression Lambda the input goes in the left (n) and the statements go on
1 the right
32
List<int> oddNums = numList.Where(n => n % 2 == 1).ToList();
2
32
foreach (int num in oddNums) {
3
Console.Write(num + ", ");
32
}
4
32
// ---------- FILE I/O ---------5
// The StreamReader and StreamWriter allows you to create text files while reading and
32
// writing to them
6
32
string[] custs = new string[] { "Tom", "Paul", "Greg" };
7
32
using (StreamWriter sw = new StreamWriter("custs.txt"))
8
{
32
foreach(string cust in custs)
9
{
33
sw.WriteLine(cust);
0
}
33
}
1
33
string custName = "";
2
using (StreamReader sr = new StreamReader("custs.txt"))
33
{
3
while ((custName = sr.ReadLine()) != null)
33
{
4
Console.WriteLine(custName);
33
}
5
}
33
6
Console.Write("Hit Enter to Exit");
33
string exitApp = Console.ReadLine();
7
33
}
8
}
33 }
9
34
0
34
1

34
2
34
3
34
4
34
5
34
6
34
7
34
8
34
9
35
0
35
1
35
2
35
3
35
4
35
5
35
6
35
7
35
8
35
9
36
0
36
1
36
2
36
3
36
4
36
5
36
6
36
7
36
8
36
9
37
0
37
1
37
2
37
3
37
4

37
5
37
6
37
7
37
8
37
9
38
0
38
1
38
2
38
3
38
4
38
5
38
6
38
7
38
8
38
9
39
0
39
1
39
2
39
3
39
4
39
5
39
6
39
7
39
8
39
9
40
0
40
1
40
2
40
3
40
4
40
5
40
6
40
7

40
8
40
9
41
0
41
1
41
2
41
3
41
4
41
5
41
6
41
7
41
8
41
9
42
0
42
1
42
2
42
3
42
4
42
5
42
6
42
7
42
8
42
9
43
0
43
1
43
2
43
3
43
4
43
5
43
6
43
7
43
8
43
9
44
0

44
1
44
2
44
3
44
4
44
5
44
6
44
7
44
8
44
9
45
0
45
1
45
2
45
3
45
4
45
5
45
6
45
7
45
8
45
9
46
0
46
1
46
2
46
3
46
4
46
5
46
6
46
7
46
8
46
9
47
0
47
1
47
2
47
3

47
4
47
5
47
6
47
7
47
8
47
9
48
0
48
1
48
2
48
3
48
4
48
5
48
6
48
7
48
8
48
9
49
0
49
1
49
2
49
3
49
4
49
5
49
6
49
7
49
8
49
9
50
0
50
1
50
2
50
3
50
4
50
5
50
6

50
7
50
8
50
9
51
0
51
1
51
2
51
3
51
4
51
5
51
6
51
7
51
8
51
9
52
0
52
1
52
2
52
3
52
4
52
5
52
6
52
7
52
8
52
9
53
0
53
1
53
2
53
3
53
4
53
5
53
6
53
7
53
8
53
9

54
0
54
1
54
2
54
3
54
4
54
5
54
6
54
7
54
8
54
9
55
0
55
1
55
2
55
3
55
4
55
5
55
6
55
7
55
8
55
9
56
0
56
1
56
2
56
3
56
4
56
5
56
6
56
7
56
8

using
using
using
using
using
using

System;
System.Collections.Generic;
System.IO;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace ConsoleApplication2
{
class Animal
{
// public : Access is not limited
// protected : Access is limited to the class methods and subclasses
// private : Access is limited to only this classes methods
public double height { get; set; }
public double weight { get; set; }
public string sound { get; set; }
// We can either have C# create the getters and setters or create them ourselves to verify
data
public string name;
public string Name
{
get { return name; }
set { name = value; }
}
// Every object has a default constructor that receives no attributes
// The constructor initializes every object created
// this is used to refer to this objects specific fields since we don't know the objects given
name
// The default constructor isn't created if you create any other constructor
public Animal()
{
this.height = 0;
this.weight = 0;
this.name = "No Name";
this.sound = "No Sound";
numOfAnimals++;
}
// You can create custom constructors as well
public Animal(double height, double weight, string name, string sound)
{
this.height = height;
this.weight = weight;
this.name = name;
this.sound = sound;
numOfAnimals++;
}
// A static fields value is shared by every object of the Animal class
// We declare thinsg static when it doesn't make sense for our object to either have the
property or
// the capability to do something (Animals can't count)
static int numOfAnimals = 0;
// A static method cannot access non-static class members
public static int getNumOfAnimals()
{
return numOfAnimals;
}

// Declare a method
public string toString()
{
return String.Format("{0} is {1} inches tall, weighs {2} lbs and likes to say {3}", name,
height, weight, sound);
}
// Overloading methods works if you have methods with different attribute data types
// You can give attributes default values
public int getSum(int num1 = 1, int num2 = 1)
{
return num1 + num2;
}
public double getSum(double num1, double num2)
{
return num1 + num2;
}
static void Main(string[] args)
{
// Create an Animal object and call the constructor
Animal spot = new Animal(15, 10, "Spot", "Woof");
// Get object values with the dot operator
Console.WriteLine("{0} says {1}", spot.name, spot.sound);
// Calling a static method
Console.WriteLine("Number of Animals " + Animal.getNumOfAnimals());
// Calling an object method
Console.WriteLine(spot.toString());
Console.WriteLine("3 + 4 = " + spot.getSum(3, 4));
// You can assign attributes by name
Console.WriteLine("3.4 + 4.5 = " + spot.getSum(num2: 3.4, num1: 4.5));
// You can create objects with an object initializer
Animal grover = new Animal
{
name = "Grover",
height = 16,
weight = 18,
sound = "Grrr"
};
Console.WriteLine(grover.toString());
// Create a subclass Dog object
Dog spike = new Dog();
Console.WriteLine(spike.toString());
spike = new Dog(20, 15, "Spike", "Grrr Woof", "Chicken");
Console.WriteLine(spike.toString());
// One way to implement polymorphism is through an abstract class
Shape rect = new Rectangle(5, 5);
Shape tri = new Triangle(5, 5);
Console.WriteLine("Rect Area " + rect.area());
Console.WriteLine("Trit Area " + tri.area());
// Using the overloaded + on 2 Rectangles

Rectangle combRect = new Rectangle(5, 5) + new Rectangle(5, 5);


Console.WriteLine("combRect Area = " + combRect.area());
// ---------- GENERICS ---------// With Generics you don't have to specify the data type of an element in a class or
method
KeyValue<string, string> superman = new KeyValue<string, string>("","");
superman.key = "Superman";
superman.value = "Clark Kent";
superman.showData();
// Now use completely different types
KeyValue<int, string> samsungTV = new KeyValue<int, string>(0, "");
samsungTV.key = 123456;
samsungTV.value = "a 50in Samsung TV";
samsungTV.showData();
Console.Write("Hit Enter to Exit");
string exitApp = Console.ReadLine();
}
}
class Dog : Animal
{
public string favFood { get; set; }
// Set the favFood default and then call the Animal super class constructor
public Dog() : base()
{
this.favFood = "No Favorite Food";
}
public Dog(double height, double weight, string name, string sound, string favFood) :
base(height, weight, name, sound)
{
this.favFood = favFood;
}
// Override methods with the keyword new
new public string toString()
{
return String.Format("{0} is {1} inches tall, weighs {2} lbs, likes to say {3} and eats
{4}", name, height, weight, sound, favFood);
}
}
// Abstract classes define methods that must be defined by derived classes
// You can only inherit one abstract class per class
// You can't instantiate an abstract class
abstract class Shape
{
public abstract double area();
// An abstract class can contain complete or default code for methods
public void sayHi()
{
Console.WriteLine("Hello");
}
}
// A class can have many interfaces
// An interface can't have concrete code
public interface ShapeItem

{
double area();
}
class Rectangle : Shape
{
private double length;
private double width;
public Rectangle( double num1, double num2)
{
length = num1;
width = num2;
}
public override double area()
{
return length * width;
}
// You can redefine many built in operators so that you can define what happens when you
// add to Rectangles
public static Rectangle operator+ (Rectangle rect1, Rectangle rect2)
{
double rectLength = rect1.length + rect2.length;
double rectWidth = rect1.width + rect2.width;
return new Rectangle(rectLength, rectWidth);
}
}
class Triangle : Shape
{
private double theBase;
private double height;
public Triangle(double num1, double num2)
{
theBase = num1;
height = num2;
}
public override double area()
{
return .5 * (theBase * height);
}
}
// ---------- GENERIC CLASS ---------class KeyValue<TKey, TValue>
{
public TKey key { get; set; }
public TValue value { get; set; }
public KeyValue(TKey k, TValue v)
{
key = k;
value = v;
}
public void showData()
{
Console.WriteLine("{0} is {1}", this.key, this.value);

}
}
}

You might also like