Unit 1: Introduction to C# PRACTICAL UNDERSTANDING Flashcards

1
Q

SYNTAX AT THE BEGINNING OF THE PROGRAM

A

using System;

namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

using System

A

means that we can use classes from the System namespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A blank line.

A

C# ignores white space. However, multiple lines makes the code more readable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

namespace

A

is used to organize your code, and it is a container for classes and other namespaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The curly braces {}

A

marks the beginning and the end of a block of code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

class is a

A

container for data and methods, which brings functionality to your program.

Every line of code that runs in C# must be inside a class. In our example, we named the class Program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Another thing that always appear in a C# program, is the Main method.

A

Any code inside its curly brackets {} will be executed.

You don’t have to understand the keywords before and after Main. You will get to know them bit by bit while reading this tutorial.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

If you omit the using System line

A

you would have to write System.Console.WriteLine() to print/output text.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Console is a class of the

A

System namespace,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Every C# statement ends with a

A

semicolon ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

“MyClass” and “myclass” has different meaning. BECAUSE

A

C# is case-sensitive:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Single-line comments start with

A

two forward slashes//

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Multi-line comments start with

A

/*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Multi-line ends with

A

*/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

To output values or print text in C#, you can use the

A

WriteLine() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

This will output Hello World….?

A

Console.WriteLine(“Hello World!”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Console.WriteLine(9 + 3);

A

This will output 12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

There is also a method, which is similar to WriteLine().

A

Write()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

The only difference is that it does not insert a new line at the end of the output

A

Write()
IT PRINTS ON THE SAME LINE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

stores integers (whole numbers), without decimals, such as 123 or -123

A

int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

stores floating point numbers, with decimals, such as 19.99 or -19.99

A

double -

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotes

A

char

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
  • stores text, such as “Hello World”. String values are surrounded by double quotes
A

string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q
  • stores values with two states: true or false
A

bool

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

To create a variable, you must specify the type and assign it a value:
WHAT IS THE SYNTAX

A

type variableName = value;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

type variableName = value;

A

Where type is a C# type (such as int or string), and variableName is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Create a variable called name of type string and assign it the value “John”:

A

string name = “John”;
Console.WriteLine(name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Create a variable called myNum of type int and assign it the value 15:

A

int myNum = 15;
Console.WriteLine(myNum);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

You can also declare a variable without assigning the value, and assign the value later:

A

int myNum;
myNum = 15;
Console.WriteLine(myNum);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

int myNum = 15;
myNum = 20; // myNum is now 20
Console.WriteLine(myNum);

A

if you assign a new value to an existing variable, it will overwrite the previous value:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

A demonstration of how to declare variables of other types:

A

int myNum = 5;
double myDoubleNum = 5.99D;
char myLetter = ‘D’;
bool myBool = true;
string myText = “Hello”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

If you don’t want others (or yourself) to overwrite existing values…

A

you can add the const keyword in front of the variable type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

If you don’t want others (or yourself) to overwrite existing values…

A

const int myNum = 15;
myNum = 20; // error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

string name = “John”;
Console.WriteLine(“Hello “ + name);

A

To combine both text and a variable, use the + character:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

string firstName = “John “;
string lastName = “Doe”;
string fullName = firstName + lastName;
Console.WriteLine(fullName);

A

You can also use the + character to add a variable to another variable:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

int x = 5;
int y = 6;
Console.WriteLine(x + y); // Print the value of x + y

A

For numeric values, the + character works as a mathematical operator (notice that we use int (integer) variables here):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

int x = 5;
int y = 6;
Console.WriteLine(x + y); // Print the value of x + y

A

x stores the value 5
y stores the value 6
Then we use the WriteLine() method to display the value of x + y, which is 11

38
Q

int x = 5, y = 6, z = 50;
Console.WriteLine(x + y + z);

A

To declare more than one variable of the same type, use a comma-separated list:

39
Q

int x, y, z;
x = y = z = 50;
Console.WriteLine(x + y + z);

A

You can also assign the same value to multiple variables in one line:

40
Q

The general rules for naming variables are:

A

Names can contain letters, digits and the underscore character (_)

Names must begin with a letter

Names should start with a
lowercase letter and it cannot contain whitespace

Names are case sensitive (“myVar” and “myvar” are different variables)

Reserved words (like C# keywords, such as int or double) cannot be used as names

41
Q

Implicit Casting

A

Implicit casting is done automatically when passing a smaller size type to a larger size type:

42
Q

int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double

Console.WriteLine(myInt); // Outputs 9
Console.WriteLine(myDouble); // Outputs 9

A

Implicit casting is done automatically when passing a smaller size type to a larger size type:

43
Q

Explicit Casting
Explicit casting must be done manually by placing the type in parentheses in front of the value:

A

double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int

Console.WriteLine(myDouble); // Outputs 9.78
Console.WriteLine(myInt); // Outputs 9

44
Q

Type Conversion Methods
?

A

It is also possible to convert data types explicitly by using built-in methods, such as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long):

44
Q

Type Conversion Methods
?

A

It is also possible to convert data types explicitly by using built-in methods, such as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long):

45
Q

It is also possible to convert data types explicitly by using built-in methods, such as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long):

A

int myInt = 10;
double myDouble = 5.25;
bool myBool = true;

Console.WriteLine(Convert.ToString(myInt)); // convert int to string
Console.WriteLine(Convert.ToDouble(myInt)); // convert int to double
Console.WriteLine(Convert.ToInt32(myDouble)); // convert double to int
Console.WriteLine(Convert.ToString(myBool)); // convert bool to string

46
Q

// Type your username and press enter

A

Console.WriteLine(“Enter username:”);

47
Q

The Console.ReadLine() method returns a string. Therefore, you cannot get information from another data type, such as int. The following program will cause an error:

A

Console.WriteLine(“Enter your age:”);
int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“Your age is: “ + age);

48
Q

+ Addition

A

Adds together two values x + y

49
Q
  • Subtraction
A

Subtracts one value from another x - y

50
Q
  • Multiplication
A

Multiplies two values x * y

51
Q

/ Division

A

Divides one value by another x / y

52
Q

% Modulus

A

Returns the division remainder x % y

53
Q

++ Increment

A

increase a value by 1

54
Q

++ Increment

A

Increases the value of a variable by 1 x++

55
Q

– Decrement

A

Decreases the value of a variable by 1 x–

56
Q

x = 5 is the same thing as?

A

x = 5

57
Q

x += 3 is the same thing as?

A

x = x + 3

58
Q

x -= 3

A

x = x - 3

59
Q

x *= 3

A

x = x * 3

60
Q

x /= 3

A

x = x / 3

61
Q

x %= 3

A

x = x % 3

62
Q

x &= 3

A

x = x & 3

63
Q

x |= 3

A

x = x | 3

64
Q

x ^= 3

A

x = x ^ 3

65
Q

x&raquo_space;= 3

A

x = x&raquo_space; 3

66
Q

x «= 3

A

x = x &laquo_space;3

67
Q

x == y

A

== Equal to

68
Q

!= x != y

A

Not equal

69
Q

>

x > y
A

Greater than

70
Q

< x < y

A

Less than

71
Q

> = x >= y

A

Greater than or equal to

72
Q

<= x <= y

A

Less than or equal to

73
Q

&& Logical and

A

Returns True if both statements are true

74
Q

|| Logical or

A

Returns True if one of the statements is true

75
Q

! Logical not

A

Reverse the result, returns False if the result is true

76
Q

IF STATEMENT EXAMPLE

A

if (20 > 18)
{
Console.WriteLine(“20 is greater than 18”);
}

77
Q

IF… ELSE STATEMENT

A

int time = 20;
if (time < 18)
{
Console.WriteLine(“Good day.”);
}
else
{
Console.WriteLine(“Good evening.”);
}
// Outputs “Good evening.”

78
Q

Short Hand If…Else (Ternary Operator)

A

variable = (condition) ? expressionTrue : expressionFalse;

79
Q

C# Switch Statements EXAMPLE

A

int day = 4;
switch (day)
{
case 1:
Console.WriteLine(“Monday”);
break;
case 2:
Console.WriteLine(“Tuesday”);
break;
case 3:
Console.WriteLine(“Wednesday”);
break;
case 4:
Console.WriteLine(“Thursday”);
break;
case 5:
Console.WriteLine(“Friday”);
break;
case 6:
Console.WriteLine(“Saturday”);
break;
case 7:
Console.WriteLine(“Sunday”);
break;
}
// Outputs “Thursday” (day 4)

80
Q

wHILE LOOP

A

int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}

81
Q

WHILE LOOP SYNTAX

A

while (condition)
{
// code block to be executed
}

82
Q

C# For Loop

A

for (statement 1; statement 2; statement 3)
{
// code block to be executed
}

83
Q

C# Break
The break statement can also be used to jump out of a loop.

A

for (int i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
Console.WriteLine(i);
}

84
Q

C# Continue

A

for (int i = 0; i < 10; i++)
{
if (i == 4)
{
continue;
}
Console.WriteLine(i);
}

85
Q

METHOD??

A

A method is a block of code which only runs when it is called.

86
Q

METHOD ARE FUNCTIONS?

A

Methods are used to perform certain actions, and they are also known as functions.

87
Q

A method is defined with the name of the method, followed by parentheses ()

A

class Program
{
static void MyMethod()
{
// code to be executed
}
}

88
Q

IF THE CLASS IS FRUIT

A
89
Q

HER

A