Rocket Launch
Hello World
class Program {
static void Main() {
System.Console.WriteLine("Hello World");
}
}
Line 1: A class called Program
Line 2: A method executed on program start
Line 3: A method-call to show text in the console
WriteLine-Method: Writes the specified data, followed by the current line terminator, to the standard output stream.
What are standard streams?
In most cases this means the output on the console. There are three types of streams: Input, Output, Error.
class Program {
static void Main() {
System.Console.WriteLine("Hello World");
}
}
}
Keywords: class, static, void;
Line 3: Calling the method ( WriteLine() ) from a class ( Console ).
Variables
- In C# a variable refers to a memory location
- As type-safe language we define the datatype when decalring a variable
Declaration:
int wholeNumber;
string characterband;
float fractionalNumber;
object smartphone;
Declaration & Initialization:
int wholeNumber = 1;
string characterband = "Some Text";
float fractionalNumber = 0.4321;
object smartphone = new object();
Resources:
Exercise
Output / Input Text
- Write a Programm that shows the text "Hello World" on the console.
- Store the text "Hello World" in a variable of type string. Print this variable on the console.
- Store a user input in a variable of type string. Print this variable on the console.
Output Formating Console
Console.WriteLine("first line");
Console.WriteLine(); // Empty Line
Console.WriteLine("third line");
The next examples print "Hello World" on the console.
string s = "Hello World";
Console.WriteLine(s);
string txt01 = "Hello";
string txt02 = "World";
Console.WriteLine(txt01 + " " + txt02);
string txt01 = "Hello";
string txt02 = "World";
Console.WriteLine("{0} {1}", txt01, txt02);
string txt01 = "Hello";
string txt02 = "World";
Console.WriteLine($"{txt01} {txt02}");
Soon we will need, to format the text we print on the console. For example alligning text to the right or displaying a certain amount of digits.
composite formattingOperators
Exercise
Add Numbers
- Write a Programm that stores the numbers 123 and 543 in two variables "num1" and "num2".
- Print the sum and the product of both values on the console.
-
Store a user input in a variable of type integer. Print the value divided by two on the console.
// Storing a number from userinput string userinput = Console.ReadLine(); int number = Convert.ToInt32(userinput);
Shorter:
int number = Convert.ToInt32( Console.ReadLine() );
Branches
For controlling the flow of the program we first have to learn about control structures.
But before we go there, we have to understand the most fundamental part:
Expressions
Something like:
( ( num < 10 ) && ( num > -10 ) );
An expression can be evaluated to a single value. Expressions can consists of operators, operands, and much more stuff. They range from very simple to very complex.
When creating control flow we use expressions, which are evaluated to a basic booblen value: true or false
Examples If Branch
int days_to_christmas = 20;
if(days_to_christmas < 5){
Console.WriteLine("Get presents!!!");
}
Loops
Exercise
Display the number from 1 to 99 on the console.
using System;
namespace Christian {
class Count {
static void Main() {
Console.WriteLine(1);
Console.WriteLine(2);
// ....
Console.WriteLine(97);
Console.WriteLine(98);
Console.WriteLine(99);
}
}
}
A lot of redundancy and +100 lines of code. There are more elegant ways:
While-Loop
using System;
namespace Christian {
class Count {
static void Main() {
int counter = 1;
while (counter < 100) {
Console.WriteLine(counter);
counter = counter + 1;
}
}
}
}
A while-loop runs a piece of code, as long as it's condition (Line 6: counter less than 100) is true.
For-Loop
using System;
namespace Christian {
class Count {
static void Main() {
for (int i = 1; i < 100; i++) {
Console.WriteLine(i);
}
}
}
}
A for-loop is essentialy the same, as the while loop, but with less line codes.
Exercise
Use a loop (for or while) to show the even numbers between 1 and 10.
using System;
namespace Christian {
class Count {
static void Main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
Console.Write(i + " ");
}
}
}
}
}
Line 6: An if-branch, which is only executed, when it's condition (i % 2 == 0)is true
Modulo
- % is the modulo operator: identifies the remainder after a given number is divided. For example: "What weekday is in a 100 Days?"
- So we think, ok: "How often does 7 fit into a 100?" 14*7 = 98, so in 98 days would be the same day as today and in 100 days 2 days ahead.
- This is the modulo calculation: 100 % 7 = 2
- We can use this, to determine if a number is even, because the modulo 2 of any even number is always 0: 8 % 2 = 0
Other Branches: If-Else, Switch
Methods
A Method (also called function in other languages) is a block of code, which can be executed by calling the method.
using System;
namespace Christian {
class Methods {
static void MyMethod() {
Console.WriteLine("Method Line 1");
Console.WriteLine("Method Line 2");
Console.WriteLine("Method Line 3");
}
static void Main() {
Console.WriteLine("Hello");
MyMethod();
Console.WriteLine("Hi");
MyMethod();
}
}
}
Line 4: Declaration of a method with the name "MyMethod" (Method-Head)
Line 5-7: Block of Code (Method-Body)
Line 11,13: Calling the method, executing it's code block
Methods can have passed-values (parameters) and a result (return-value)
This method gets passed two integer values and returns the sum
using System;
namespace Christian {
class Methods {
static int AddTwoNumbers(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
static void Main() {
int result = AddTwoNumbers(5, 7);
Console.WriteLine(result);
}
}
}
Line 4: We define a return type, which always is the keyword that stands right before the name of the method. The Parameters folow after the name in the perenthesis and are mendatory if declared here.
Line 10: Prints 12 on the console
Can we write a shorter code that does the same?
using System;
namespace Christian {
class Methods {
static int AddTwoNumbers(int num1, int num2) {
return num1 + num2;
}
static void Main() {
Console.WriteLine( AddTwoNumbers(5, 7) );
}
}
}
Code Examples
Threejs JavaScriptSidenode:
- C# --> Microsoft (2000)
- Java --> Sun Microsystems (1995)
- JavaScript --> Netscape (1995)
Description | Keybinding (english layout!) |
---|---|
Undo | Strg + Z |
Format Document | Strg + E, D |
Comment | Strg + K, C |
Uncomment | Strg + K, U |
Mark Lines | Shift + Down / Up Cursor |
Jump Cursor | Strg + Left / Right Cursor |
Mark Words | Strg + Shift + Left / Right Cursor |
Delete Words | Strg + Del / Backspace |
Duplicate Line (cursor blinking in line) | Strg + C, Strg + V |
Start Debug | F5 |
Insert Empty Line Above | Strg + Enter |
Insert Empty Line Below | Strg + Shift + Enter |
Zoom Code | Strg + Mousewheel Up / Down |
Collapse All Code | Strg + M, O |
Rename Element (must be selected) | F2 or Strg + R, R |