diff --git a/1-introduction-to-csharp-and-dotnet-slides.html b/1-introduction-to-csharp-and-dotnet-slides.html new file mode 100644 index 0000000..3cd5f48 --- /dev/null +++ b/1-introduction-to-csharp-and-dotnet-slides.html @@ -0,0 +1,147 @@ +
double airPressure = 1.2; // Use for most decimal numbers
+decimal accountBalance = 1.2m; // Use for accuracy (e.g. financial applications)
+float bulletSpeed = 1.2f; // Use only when you know its precision will be enough
+bool loggedIn = false;
+char previousInput = 'b';
+
+char
is only used for single characters, multi-character strings will be introduced in a bit.Data types can be cast to another either...
+...implicitly:
+double valueAddedTax = 25.5;
+decimal valueAddedTaxDecimal = valueAddedTax;
+
+...explicitly:
+double valueAddedTax = 25.5;
+decimal valueAddedTaxDecimal = (decimal)valueAddedTax;
+
+double
and a decimal
together:double a = 1.0;
+decimal b = 2.1m;
+Console.WriteLine(a + (double)b);
+Console.WriteLine((decimal)a + b);
+
+Operator | +Name | +Example | +Description | +
---|---|---|---|
+ |
+Addition | +a + b |
+Adds together two values | +
- |
+Subtraction | +a - b |
+Subtracts one value from another | +
* |
+Multiplication | +a * b |
+Multiplies two values | +
/ |
+Division | +a / b |
+Divides one value by another | +
% |
+Modulus | +a % b |
+Returns the division remainder | +
++ |
+Increment | +a++ |
+Increases the value by 1 | +
-- |
+Decrement | +a–- |
+Decreases the value by 1 | +
double
.We have used the assignment operator =
for assigning values for variables:
int x;
+x = 25;
+
+=
and ==
introduced in Lecture 3!
+=
is used for assigning values for variables, ==
is used for comparing valuesOperator | +Example | +Same As | +
---|---|---|
= |
+x = 5 |
+x = 5 |
+
+= |
+x += 5 |
+x = x + 5 |
+
-= |
+x -= 5 |
+x = x - 5 |
+
*= |
+x *= 5 |
+x = x * 5 |
+
/= |
+x /= 5 |
+x = x / 5 |
+
%= |
+x %= 5 |
+x = x % 5 |
+
int uppercaseLetters = 2;
+uppercaseLetters += 4; // is now 6
+
+int specialCharacters = 2;
+specialCharacters *= 2; // is now 4
+
+Console.WriteLine(uppercaseLetters);
+Console.WriteLine(specialCharacters);
+
+int a = 3;
+
+a = a + 1; // a is now 4
+a += 1; // a is now 5
+a++; // a is now 6
+
+int a = 3;
+
+a = a - 1; // a is now 2
+a -= 1; // a is now 1
+a--; // a is now 0
+
+++
and --
are called the increment and decrement operators++i
) or a postfix (i++
)a++
and ++a
do exactly the same:int a = 3;
+a++; // a is now 4
+++a; // a is now 5
+
+int a = 3;
+int b = ++a;
+Console.WriteLine(b); // 4
+
+Assignment of b
happens after ++
,
+so its value is 4
int a = 3;
+int b = a++;
+Console.WriteLine(b); // 3
+
+Assignment of b
happens before ++
,
+so its value is 3
String is a special type, which contains an array of characters. You can declare and assign strings like any other type of variables:
+string name = "Johannes Kantola";
+You can concatenate multiple strings with the '+' operator:
+string firstName = "Johannes";
+string lastName = "Kantola";
+string fullName = firstName + " " + lastName;
+Console.WriteLine(fullName); // Outputs "Johannes Kantola"
+Character constants are preceded by a backslash '\' and can be used for formatting strings
+'\n' represents a newline in the following example:
+string firstName = "Johannes";
+string lastName = "Kantola";
+string fullName = firstName + "\n" + lastName;
+Console.WriteLine(fullName);
+/* This outputs
+Johannes
+Kantola
+*/
+All character constants: https://www.tutorialspoint.com/csharp/csharp_constants.htm
+Concatenating multiple variables into one string with the '+' operator quickly becomes tedious
+It's much easier to use __string interpolation __ by prefixing your target string with '$' and inserting the variables inside curly brackets '{ }':
+string animal = "Dog";
+string sound = "Woof";
+Console.WriteLine($"{animal} says {sound}!");
+// Outputs "Dog says Woof!"
+You can add format strings to change the way variables are interpolated into a string
+Add the format string after your variable, separated by a colon (:)
+You can find an overview of format strings and a handy list of both standard and custom strings here
+double pi = 3.141592653;
+Console.WriteLine($"Pi to three digits: {pi:G3}");
+// Outputs "Pi to three digits: 3.14"
+For the next exercise, you'll need the Console.ReadLine() method. The method pauses the program, waits for an input stream from the console that pops up, and returns the value of the input:
+string userInput = Console.ReadLine();Console.WriteLine(userInput);
+Create a console application which asks the user which weekday it is and assigns the answer to a string variable.
+Print "Have a nice weekday!" to the console where weekday is replaced with the string the user wrote.
+