| `^` | XOR | `a ^ b` | Exclusive OR ("*joko tai*"): returns `true` if *__only either of__*`a` or `b` are `true`, but not both! |
| `&` | Logical AND | `a & b` | Returns `true` if __both__ variables are `true`.<br>Both variables are always evaluated. |
| `\|` | Logical OR | `a \| b` | Returns `true` if __one or both__ variables are `true`.<br>Both variables are always evaluated. |
# Operators - Example
### Logical operators: An Example
This outputs "Strong password!"
```csharp
int uppercaseLetters = 2;
uppercaseLetters += 4; // is now 6
int specialCharacters = 2;
specialCharacters *= 2; // is now 4
if (uppercaseLetters >= 6 && specialCharacters >= 2)
{
Console.WriteLine("Strong password!");
Console.WriteLine("Strong password!");
}
else
{
Console.WriteLine("Weak-ass password...");
Console.WriteLine("Weak password...");
}
```
This outputs `Strong password!`
# Exercise 1:
## Extra: Single statement `if`
<!-- _class: "extra invert" -->
Create a console application which asks the user which weekday it is and assigns the answer to a string variable.
* If a code block following a statement only has one line of code, it is possible to write the block ***without*** curly brackets:
```csharp
int baa = 49;
if (baa > 20)
Console.WriteLine("Baa");
else
Console.WriteLine("Not baa!");
if (baa > 20) Console.WriteLine("Baa");
else Console.WriteLine("Not baa!");
```
* You may encounter code like this — however, we highly recommend to refrain from using this syntax as it is highly prone to [errors](https://www.leadingagile.com/2018/01/the-goto-fail-bug-as-a-coaching-tool/).
* Can you guess how this syntax can lead to bugs?
Using a switch-case, calculate the days remaining until next Monday.
If the result is more than 3, print "Have a nice week!". Otherwise, print "Have a nice weekend!".
# Exercise 2:
Create a console application which lets the user input a note.
If the length of the note is less than 30, the program prints the current time and the note, separated with a tab. Otherwise, the date and the note are printed to a separate line.
Tip: Use DateTime.Now.ToString() for current time. Use .Length after your message variable to get the length of the message.
# One More Thing...
If a code block following a statement only has one line of code, you can write the code without using curly brackets:
int baa = 49;
if (baa > 20)
Console.WriteLine("Baa");
else
## Exercise 1
<!--_class: "exercise invert" -->
Console.WriteLine("Not baa!");
1) Create a console application that asks the user which weekday it is and assigns the answer to a string variable.
2) Using a switch-case expression, calculate the days remaining until next Monday.
3) If the result is more than 3, print `Have a nice week!`. Otherwise, print `Have a nice weekend!`.
if (baa > 20) Console.WriteLine("Baa");
## Exercise 2
<!--_class: "exercise invert" -->
else Console.WriteLine("Not baa!");
1) Create a console application that lets the user input a note as a string.
2) If the length of the note is less than 30, the program prints the current time and the note separated by a tab. Otherwise, the date and the note are printed to a separate line.
You may see code where this is done. However, we highly recommend you not to use this syntax as it is highly prone to [errors](https://www.leadingagile.com/2018/01/the-goto-fail-bug-as-a-coaching-tool/) .
***Tip:*** Use `DateTime.Now.ToString()` for current time. Use `.Length` after your message variable to get the length of the message.
# Get Help
All the basics covering the syntax in C# are covered here:
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/3.%20Conditionals)
<lidata-marpit-fragment="1">Using namespaces helps you to organize the scope of your project, so that...
@ -44,7 +44,7 @@
<lidata-marpit-fragment="5">By default, when creating a new project and adding classes in Visual Studio, everything is contained within a namespace named after your project name</li>
<lidata-marpit-fragment="1">If you need a class, interface, etc to be accessible from anywhere, create it within that namespace and set it as <code>public</code></li>
</ul>
@ -74,7 +74,7 @@
<lidata-marpit-fragment="2">Wait, what's a class, though?</li>
<lidata-marpit-fragment="1"><ahref="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes">Classes</a> in C# are blueprints for specific kinds of collections of data & functionality</li>
<lidata-marpit-fragment="1">Classes are a core feature of the <em><strong>object-oriented programming</strong></em> paradigm popularized by Java in the 90s
@ -105,7 +105,7 @@
<lidata-marpit-fragment="6">Shorter files can also mean that it's hard to find the actual functionality from hundreds of short files full of intermediate classes and class factories</li>
<lidata-marpit-fragment="1">You can create a new class by writing<preis="marp-pre"data-auto-scaling="downscale-only"><codeclass="language-csharp"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">Student</span>
@ -124,7 +124,7 @@
<lidata-marpit-fragment="3">These variables declared directly inside classes are called <em><strong>fields</strong></em>.</li>
<p>Let's fix the previous example by changing the <em><strong>access specifier</strong></em> of the variable <code>id</code> to <code>public</code>:</p>
<divclass='columns'markdown='1'>
<divmarkdown='1'>
@ -215,34 +215,46 @@ Student class:
<lidata-marpit-fragment="1">The value of the variable <code>id</code> of the object <code>student</code> is now <code>12345678</code></li>
<lidata-marpit-fragment="1"><em><strong>Access specifiers</strong></em> can be used to get additional level of protection inside classes</li>
<lidata-marpit-fragment="2">Variables specified with <code>private</code> are accessible only inside the containing class
<lidata-marpit-fragment="1"><a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers">Access modifiers</a> can be used to get additional level of protection inside classes</li>
<lidata-marpit-fragment="2"><code>private</code>: accessible only inside the containing class
<ul>
<lidata-marpit-fragment="3">This is the <em><strong>default</strong></em> access!</li>
<lidata-marpit-fragment="3">This is the default, but we can make it more explicit by writing it out</li>
</ul>
</li>
<lidata-marpit-fragment="4">Variables specified with <code>public</code> are accessible outside of the class<preis="marp-pre"data-auto-scaling="downscale-only"><codeclass="language-csharp"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">Student</span>
<lidata-marpit-fragment="4"><code>public</code>: accessible everywhere in the namespace</li>
<lidata-marpit-fragment="5">Less common, but good to know:
<ul>
<lidata-marpit-fragment="6"><code>protected</code>: like <code>private</code>, but also accessible by the <em>inheritors</em> of the class</li>
<lidata-marpit-fragment="7"><code>virtual</code>: accessible and <em>overridable</em> by inheritors</li>
<spanclass="hljs-built_in">int</span> id; <spanclass="hljs-comment">// Accessible only inside the class</span>
<spanclass="hljs-keyword">private</span><spanclass="hljs-built_in">string</span> name; <spanclass="hljs-comment">// Accessible only inside the class</span>
<spanclass="hljs-keyword">public</span><spanclass="hljs-built_in">string</span> address; <spanclass="hljs-comment">// Accessible everywhere within the namespace</span>
<p>Continuing on the class in the previous example: if we follow the <code>student</code> variable with a dot, Visual Studio IntelliSense will only suggest the <code>address</code> variable, because it was the only <code>public</code> variable of the <code>Student</code> class!</p>
<p>Continuing on the class in the previous example: if we follow the <code>student</code> variable with <code>.</code>, Visual Studio IntelliSense will only suggest the <code>address</code> variable, because it was the only <code>public</code> variable of the <code>Student</code> class!</p>
<lidata-marpit-fragment="1">Constructors are class methods which are called once the object is initialized</li>
@ -354,7 +366,7 @@ Student class:
<lidata-marpit-fragment="3"><em><strong>Note:</strong></em> In Visual Studio, just write <code>ctor</code> and press tab twice to quickly create a constructor!</li>
<lidata-marpit-fragment="1">When working with C#, you will eventually see <ahref="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties">properties</a> being used at some point</li>
<lidata-marpit-fragment="1">Auto properties are a shorthand version of the same:</li>
@ -472,7 +484,7 @@ Student class:
<lidata-marpit-fragment="2"><em><strong>Note:</strong></em> In Visual Studio, just write <code>prop</code> and press tab twice to quickly create an auto property</li>
* The value of the variable `id` of the object `student` is now `12345678`
## Access Specifiers
## Access modifiers
* *__Access specifiers__* can be used to get additional level of protection inside classes
* Variables specified with `private` are accessible only inside the containing class
* This is the ***default*** access!
* Variables specified with `public` are accessible outside of the class
```csharp
class Student
{
int id; // Accessible only inside the class
private string name; // Accessible only inside the class
public string address; // Accessible everywhere within the namespace
}
```
<divclass='columns21'markdown='1'>
<divmarkdown='1'>
* [Access modifiers](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers) can be used to get additional level of protection inside classes
* `private`: accessible only inside the containing class
* This is the default, but we can make it more explicit by writing it out
* `public`: accessible everywhere in the namespace
* Less common, but good to know:
* `protected`: like `private`, but also accessible by the *inheritors* of the class
* `virtual`: accessible and *overridable* by inheritors
</div>
<divmarkdown='1'>
```csharp
class Student
{
int id;
private string name;
public string address;
}
```
</div>
</div>
---
* Continuing on the class in the previous example: if we follow the `student` variable with a dot, Visual Studio IntelliSense will only suggest the `address` variable, because it was the only `public` variable of the `Student` class!
* Continuing on the class in the previous example: if we follow the `student` variable with `.`, Visual Studio IntelliSense will only suggest the `address` variable, because it was the only `public` variable of the `Student` class!