C# Fundamentals

studied byStudied by 3 people
5.0(1)
Get a hint
Hint

What is an IDE?

1 / 84

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

85 Terms

1

What is an IDE?

An integrated development environment (IDE) is a development tool that helps programmers write, build, compile, and debug their programs.

New cards
2

What is compilation?

Compilation is the process of converting code from human-readable text into a format, like binary, that a computer can understand and execute.

New cards
3

What is a compiler?

A compiler is a tool that translates code into a language that the computer can understand and run.

New cards
4

What are .dll files?

DLL stands for Dynamic Link Library. These files contain compiled code in binary format, which is used by executable (.exe) files. If the .dll is missing, the .exe won't function properly.

New cards
5

What happens if a .dll file is removed?

If a .dll file is deleted, the associated executable file (.exe) won't run because it relies on the code inside the .dll.

New cards
6

What is the result of C# code compilation?

C# code is compiled into .dll files, which contain the program in a format that can be executed by a computer.

New cards
7

What is the most important skill a developer should have?

The ability to find solutions for issues.

New cards
8

What are variables?

Variables store values, and each variable is assigned a type that cannot be changed after it's declared. Only the value can be updated later.

New cards
9

What is the int type used for?

The int type is used for storing whole numbers.

New cards
10

What is the string type used for?

The string type is used for storing text.

New cards
11

What is the difference between declaration and initialization?

Declaration is when we specify the type and name of the variable, while initialization is when we assign a value to that declared variable.

New cards
12

Can declaration and initialization be done at the same time?

Yes, we can declare and initialize a variable in one step by assigning a value at the time of declaration:

int number = 5;

New cards
13

Can a variable be used before it is both declared and initialized?

No, a variable cannot be used until it has been declared and initialized.

New cards
14

Can we declare multiple variables on a single line?

Yes, we can declare multiple variables in one line, such as:

int a = 1, b = 6;
string name, lastName;

New cards
15

How do we name variables in C#?

  • Variables can contain letters, digits, and underscores

  • Variables must begin with a letter.

  • C# is case-sensitive

  • camelCase is recommended

  • Names should be meaningful and precise

New cards
16

Can we use reserved keywords as variable names in C#?

Reserved keywords cannot be used as variable names, but they can be bypassed by adding an @ symbol before the keyword, such as

string @class = "First";

New cards
17

What are reserved keywords?

Reserved keywords are special words used to give instructions in C#, and they cannot be used directly as variable names.

New cards
18

Why is clean code important?

Clean code improves readability and helps others (and yourself) understand the code more quickly. It includes using meaningful, precise variable names and avoiding abbreviations.

New cards
19

What are some best practices for naming variables?

To write clean code, avoid abbreviations, use precise names, and take your time naming variables. Variables can also be renamed later if a better name becomes clear.

New cards
20

What are the most basic operators in C#?

The most basic operators are +, -, *, /, ++, and --. These are used for addition, subtraction, multiplication, division, and increment/decrement, respectively.

New cards
21

What can the + operator be used for in C#?

The + operator can be used for both adding numbers and concatenating strings, like "John" + "Smith".

New cards
22

What is operator precedence in C#?

Operator precedence determines the order in which operations are evaluated in an expression. Operators with higher precedence are executed first. For example, multiplication and division are evaluated before addition and subtraction.

New cards
23

How can operator precedence affect calculations?

If operators are not grouped properly using parentheses, the result may be incorrect due to precedence. For example,

Console.WriteLine("Subtraction: " + a - b);

throws an error, but using parentheses

Console.WriteLine("Subtraction: " + (a - b));

ensures correct execution.

New cards
24

What is the purpose of the var keyword in C#?

The var keyword allows for implicitly typed variables, where the compiler determines the variable's type based on the value assigned during initialization.

New cards
25

What is the difference between implicitly and explicitly typed variables?

Implicitly typed variables use the var keyword, and the type is inferred by the compiler at the time of initialization. Explicitly typed variables require you to specify the type, such as int or string, when declaring the variable.

New cards
26

Can you declare a variable using var without initializing it?

No, when using var, the variable must be initialized at the time of declaration because the type is determined based on the assigned value.

New cards
27

Can the type of a variable declared with var be changed later?

No, once a variable is declared and initialized using var, its type is fixed and cannot be changed.

New cards
28

How can we read user input in C#?

We can read user input by using the Console.ReadLine() function.

New cards
29

What are code snippets?

Code snippets are pre-written templates that allow you to quickly insert common code structures into your program.

New cards
30

What is the role of warnings in C#?

Warnings are suggestions from the compiler indicating potential issues in the code, but they don’t stop compilation like errors do. They are usually worth addressing to improve code quality.

New cards
31

How do we set breakpoints in C#?

Breakpoints can be set by clicking on the left side of the line number or by pressing F9 on the desired line of code.

New cards
32

Why do we use breakpoints in debugging?

Breakpoints pause the program execution just before a specific line of code, allowing us to inspect variable values and program behavior at that point.

New cards
33

What is QuickWatch in C#?

QuickWatch is a tool that allows you to evaluate expressions or parts of code during debugging. It shows the result of selected code when you press Shift + F9.

New cards
34

How can we add comments in C#?

Comments can be added using // for single-line comments and /* */ for multi-line comments.

New cards
35

What is the handy option involving the Alt key in Visual Studio?

By holding the Alt key and dragging the cursor up or down, you can create multiple cursors on selected lines, allowing you to edit multiple lines simultaneously.

New cards
36

What kind of data type is a bool in C#?

A bool data type can only hold two values: true or false.

New cards
37

What is the purpose of boolean operators in C#?

Boolean operators are used to check whether a specific condition is met, returning either true or false.

New cards
38

What is an example of using the equality operator with a boolean variable?

You can use the equality operator (==) to compare values, such as:

bool isUserInputAbc = userChoice == "ABC"; // checks if user input is "ABC"

New cards
39

How can you check if two values are not equal in C#?

You can check for inequality using the != operator or logical negation:

bool isUserInputNotAbc = userChoice != "ABC"; 
bool isUserInputNotAbc2 = !(userChoice == "ABC");

New cards
40

What are some comparison operators in C#?

Comparison operators include >, <, >=, and <=. For example:

var is10Modulo3EqualTo1 = 10 % 3 == 1; // evaluates to true

New cards
41

What do the && and || operators do in C#?

The && (AND) operator checks if all specified conditions are true, while the || (OR) operator checks if at least one condition is true.

New cards
42

What is short-circuiting in C#?

Short-circuiting is an optimization in evaluating logical conditions. If the first condition in an || operation is true, the remaining conditions are not evaluated. Similarly, in an && operation, if the first condition is false, the subsequent conditions are skipped.

New cards
43

Why is it advisable to place lightweight operations on the left side of logical operators?

Placing lighter operations on the left side allows for more efficient evaluations, as the program can short-circuit without evaluating heavier operations if the outcome can be determined early.

New cards
44

How can we enhance clarity when combining multiple conditions?

It’s best to use parentheses to group conditions, which makes the expression clearer and easier to understand.

New cards
45

How should boolean variables be named for better readability?

Boolean variables should be named in the form of a question, indicating what condition they are checking.

New cards
46

How do we use the if/else statement in C#?

An if/else statement evaluates a condition in parentheses. If the condition is true, the code block inside the if is executed; if it’s false, the code block inside the else is executed. For example:

Console.WriteLine("Please enter a word.");
string userChoice = Console.ReadLine();

if (userChoice == "ABC")
{
Console.WriteLine("User typed ABC.");
}
else
{
Console.WriteLine("User did not type ABC.");
}

New cards
47

What are scopes in C#?

A scope is a specific part of the program where a variable is accessible. If you try to access a variable outside its defined scope, the compiler will generate an error.

New cards
48

What are local variables, and what is their scope?

Local variables are variables defined within a code block (enclosed by {}) and can only be accessed within that block and any nested blocks. For example:

void NestedBlocks()
{
int outerVariable = 10; // Local variable
if (outerVariable > 5)
{
int innerVariable = 20; // Local variable
Console.WriteLine(innerVariable); // Accessible here
}
// Console.WriteLine(innerVariable); // Error: Cannot access innerVariable here
}

or

void Greeting()
{
string message = "Hello!"; // Local variable
Console.WriteLine(message);
}
// Console.WriteLine(message); // Error: Cannot access message here

New cards
49

Can a local variable be accessed outside its defining block?

No, a local variable cannot be accessed outside the block in which it was defined.

New cards
50

What is a method in C#?

A method is a reusable piece of code that can be executed multiple times by referring to its name. It helps avoid repetition and reduces the risk of bugs when code needs to be modified.

New cards
51

What is the difference between defining and calling a method?

Defining a method involves specifying its return type, name, parameters, and body of code. Calling a method means executing it by using its name and providing any necessary arguments.

// Defining a method
void PrintSelectedOption(string selectedOption)
{
Console.WriteLine("Selected option: " + selectedOption);
}

// Calling the method
PrintSelectedOption("Exit"); // Here, "Exit" is the argument

New cards
52

What is the difference between parameters and arguments?

Parameters are variables defined in the method declaration that specify what values the method can accept. Arguments are the actual values passed to the method when it is called. For example, in the method PrintSelectedOption(string selectedOption), selectedOption is a parameter, and when calling PrintSelectedOption("Exit"), "Exit" is the argument.

New cards
53

How do we define a non-void method in C#?

To define a non-void method, specify the return type before the method name, and use the return keyword to return that type from the method's body. The execution exits the method once it reaches the return keyword.

int Add(int a, int b)
{
return a + b; // Must return an integer
}

New cards
54

How can we debug methods in C#?

To debug methods, we can step into them by pressing F11 (or the equivalent key in your IDE). This allows us to execute the code line by line and observe how the arguments are processed.

New cards
55

When can we remove the else keyword from an if/else statement without changing the code’s behavior?

We can remove the else keyword when we use the return keyword correctly. For instance:

bool IsLong(string input)
{
if (input.Length > 10)
{
return true; // If true, exits method
}
return false; // No else needed
}

which can be further simplified:

bool IsLong(string input)
{
return input.Length > 10; // Directly returns the comparison result
}

New cards
56

What is code refactoring?

Code refactoring is the process of improving code quality without changing its external behavior. This can involve simplifying code, improving readability, and enhancing maintainability.

New cards
57

What is the difference between dynamically and statically typed languages?

In statically typed languages like C#, the compiler checks for type mismatches at compile time, and variable types cannot change once declared. In dynamically typed languages, the variable's type can change at runtime by reassigning it a value of a different type.

New cards
58

What is the difference between runtime errors and compilation errors?

Runtime errors occur during the execution of the application, causing it to crash or behave unexpectedly. Compilation errors happen during the compilation phase, preventing the application from running due to issues such as syntax errors or type mismatches.

New cards
59

What is a key advantage of using a statically typed language like C#?

A key advantage is that it helps reduce the introduction of bugs by ensuring there are no type mismatches between variables, methods, and objects before the code is executed.

New cards
60

What is parsing?

Parsing is the process of transforming a string into a different data type.

New cards
61

How do we parse a string into an int in C#?

We can parse a string into an int using the int.Parse() method, as shown below:

Console.WriteLine("Please enter a number");
string userInput = Console.ReadLine();
int number = int.Parse(userInput);

New cards
62

What method is used to convert a string to an integer?

The int.Parse() method is used to convert a string to an integer.

New cards
63

What are exceptions?

Exceptions are runtime errors in our code — once an exception occurs, the program cannot continue and it crashes.

New cards
64

What is a solution?

A solution is a collection of projects.

New cards
65

What is string interpolation?

String interpolation is a method of building strings in a more intuitive way, allowing for the inclusion of variable values directly within a string.

New cards
66

How do you use string interpolation in C#?

You can use string interpolation by placing a $ before the string and including variables in curly braces. For example:

int a = 4,
b = 2,
c = 10;

Console.WriteLine($"First is {a}, second is {b}, third is {c}.");

New cards
67

What are the advantages of string interpolation over concatenation?

String interpolation provides a more readable and intuitive way to construct strings without needing to use the + operator for concatenation, making the code cleaner and easier to understand.

New cards
68

What is a switch statement?

A switch statement allows us to replace multiple mutually exclusive if/else statements, providing a more organized way to handle different cases based on a variable's value.

var userChoice = Console.ReadLine();

switch (userChoice)
{
case "S":
case "s":
PrintSelectedOption("See all TODOs");
break;
case "A":
case "a":
PrintSelectedOption("Add a TODO");
break;
case "R":
case "r":
PrintSelectedOption("Remove a TODO");
break;
case "E":
case "e":
PrintSelectedOption("Exit");
break;
default:
Console.WriteLine("Invalid choice!");
break;
}

New cards
69

Why do we use the default keyword in a switch statement?

The default keyword provides a fallback option for the switch statement when none of the specified cases match the value being evaluated.

New cards
70

What are expressions in programming?

An expression is a piece of code that evaluates to a value.

New cards
71

What must be included in each case of a switch statement?

Each case must include the break keyword or the return keyword to prevent fall-through, meaning the next case will not be executed unless explicitly intended.

string ConverPointsToGrade(int points)
{
switch (points)
{
case 10:
case 9:
return "A";
case 8:
case 7:
case 6:
return "B";
case 5:
case 4:
case 3:
return "C";
case 2:
case 1:
return "D";
default:
return "!";
}
}

New cards
72

What is a char in C#?

A char is a data type that allows us to store only a single character. For example:

char ConverPointsToGrade(int points)
{
switch (points)
{
case 10:
case 9:
return 'A';
case 8:
case 7:
case 6:
return 'B';
case 5:
case 4:
case 3:
return 'C';
case 2:
case 1:
return 'D';
default:
return '!';
}
}

New cards
73

What are loops, and what are they useful for?

Loops allow us to execute pieces of code multiple times, which is much needed in the real world.

New cards
74

What kinds of loops can we use in C#?

The types of loops in C# are:

  • while

  • do-while

  • for

  • foreach

New cards
75

How does the while loop work in C#?

The while loop checks a condition before each iteration. If true, the code inside the loop runs; if false, the loop is skipped, and execution continues. For example:

var number = 0;
while(number < 10)
{
number++;
Console.WriteLine($"Number is {number}");
}
Console.WriteLine("The loop is finished.");

New cards
76

What happens if the condition in a while loop is false?

The loop is skipped entirely, and execution moves to the next line of code.

New cards
77

What is a key aspect of the while loop's condition?

The condition is checked with each iteration; code runs only if the condition is true.

New cards
78

What does the += operator do in C#?

The += operator updates a variable by adding a specified value to it. For example, number += 1; is equivalent to number = number + 1;.

New cards
79

Can the += operator be used with values other than 1?

Yes, the += operator can be used to add any numeric value, not just 1.

New cards
80

How do we increment a variable by 1 using a shorthand operator?

You can use the ++ operator, as in number++;, to increment a variable by 1.

New cards
81

Can other arithmetic operators be used like +=?

Yes. Other operators are -=, *=, /=.

New cards
82

Can the += operator be used with strings?

Yes, the += operator can also concatenate strings.

New cards
83

What are infinite loops?

Infinite loops are loops that execute indefinitely because their exit condition is always true.

New cards
84

Why do we need to be cautious with infinite loops?

We must ensure that the loop modifies a variable to eventually make the exit condition false, preventing an endless execution.

New cards
85
New cards

Explore top notes

note Note
studied byStudied by 5 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 107 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 3 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 23 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 9 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 20 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 1 person
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 4176 people
Updated ... ago
4.7 Stars(33)

Explore top flashcards

flashcards Flashcard21 terms
studied byStudied by 1 person
Updated ... ago
5.0 Stars(1)
flashcards Flashcard20 terms
studied byStudied by 19 people
Updated ... ago
5.0 Stars(3)
flashcards Flashcard29 terms
studied byStudied by 19 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard105 terms
studied byStudied by 56 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard26 terms
studied byStudied by 2 people
Updated ... ago
5.0 Stars(2)
flashcards Flashcard137 terms
studied byStudied by 1 person
Updated ... ago
5.0 Stars(1)
flashcards Flashcard79 terms
studied byStudied by 386 people
Updated ... ago
5.0 Stars(4)
flashcards Flashcard61 terms
studied byStudied by 5 people
Updated ... ago
5.0 Stars(1)