-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
183 lines (124 loc) · 4.11 KB
/
Program.cs
File metadata and controls
183 lines (124 loc) · 4.11 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// See https://aka.ms/new-console-template for more information
Console.Clear();
Console.WriteLine("Hello, World!"); // console.log
// SECTION strings
string firstName = "Jeremy";
firstName = "Jerms";
// firstName = 2; 🙅♂️ once it's a string, it's always a string!
var lastName = "Fowler";
// lastName = 0;
string favoriteCat = "Georgie";
char middleInitial = 'B';
char favoriteLetter = 'J';
string fullName = firstName + ' ' + middleInitial + ' ' + lastName;
Console.WriteLine($"My name is {fullName}! My favorite letter is of course the letter {favoriteLetter} and my favorite cat is named {favoriteCat}.");
// !SECTION
// SECTION numbers
int age = 34;
age = age + 1;
age += 1;
age++;
age *= 5;
age %= 2;
age /= 2;
age = 35;
Console.WriteLine($"I am {age} years old!");
int maxInt = 2147483647;
maxInt++; // -2147483648
float amountOfCoffeeSipped = 2.09090f;
double amountOfHugsGivenToDad = 3.4;
decimal amountInBank = 2.3434343434343565654656m; // use for money 💵
var someNumber = 23.2; // double
Console.WriteLine("MAXIMUM INT " + maxInt);
// !SECTION
// SECTION boolean
bool ateBreakfast = true;
bool drankCoffee = false;
// if ("7" == 7) 🙅♂️ cannot compare two different data types
if (2 + 2 == 4)
{
Console.WriteLine("Math is working");
}
// if(2 + 2) 🙅♂️ truthy falsy does not exist in C#
if (ateBreakfast && drankCoffee)
{
Console.WriteLine($"{firstName} is in a great mood");
}
else if (ateBreakfast || drankCoffee)
{
Console.WriteLine($"{firstName} is in an okay mood");
}
else
{
Console.WriteLine("Lecture is over go work on your crap");
}
// !SECTION
// SECTION null
string? nothing = null;
string noString; // defaults to null
int noNumber; // defaults to 0
bool noBool; // defaults to false
// undefined does not exist in C#
// !SECTION
// SECTION reference types [] {}
// NOTE arrays are dog water
string[] dogNames = ["dipper", "pluto", "chomp", "jackson", "hooper", "jalapeno"];
Console.WriteLine($"The first dog is named {dogNames[0]}");
for (int i = 0; i < dogNames.Length; i++)
{
string name = dogNames[i];
Console.WriteLine($"WOOF WOOF MY NAME IS {name.ToUpper()}!");
}
// NOTE Lists are more flexible and easier to use in C#
List<string> birdNames = ["louie", "tweety"];
birdNames.Add("annie"); // js push method
birdNames.Add("jake overall");
birdNames.Insert(0, "sam"); // js unshift method
birdNames.ForEach(name => Console.WriteLine($"CAW CAW MY NAME IS {name}"));
Cat gopher = new Cat("gopher", 5, true, "black", ["plant", "bread", "toilet water", "flies"]);
// Console.WriteLine(gopher.Introduction());
// Console.WriteLine(gopher._thingsEaten); 🙅♂️ cannot access private members outside of the class definition
List<Cat> cats = [
gopher,
new Cat("georgie", 10, true, "gray", ["plastic"]),
new Cat("frankie", 5, true, "black", ["cheese", "cat food"])
];
foreach (var cat in cats)
{
Console.WriteLine(cat.Introduction());
}
class Cat
{
// NOTE class members should be named with UpperPascalCase
public string Name { get; set; }
public int Age { get; set; }
public bool LikesCheese { get; set; }
public string Color { get; set; }
// NOTE cannot access private members OUTSIDE of the class definition
private string[] _thingsEaten;
// NOTE this is the constructor
// NOTE parameters should be named with lowerCamelCase
public Cat(string name, int age, bool likesCheese, string color, string[] thingsEaten)
{
this.Name = name;
// NOTE the 'this' keyword is optional
Age = age;
LikesCheese = likesCheese;
Color = color;
_thingsEaten = thingsEaten;
}
// NOTE 'string' is the return type of the method
public string Introduction()
{
// Any is equivalent to the 'some' method in js
bool threwUp = _thingsEaten.Any(thing => thing == "plant" || thing == "plastic");
return $"Hello my name is {Name}. I am {Age} years old and I {(LikesCheese ? "like" : "hate")} cheese. My fur is a beautiful shade of {Color}. {(threwUp ? "I threw up on the bed." : "I'm a good cat.")}";
}
}
// js example
// class Cat{
// constructor(name, age){
// this.name = name
// this.age = age
// }
// }