2. Core C# Flashcards

1
Q

What do statements end with?

A

Statements end in a semicolon (;) and can continue over multiple lines without needing a continuation character.

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

How do you join statements into blocks?

A

Statements can be joined into blocks using curly braces ({}).

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

How do you create a single line comment?

A

Single-line comments begin with two forward slash characters (//).

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

How are multiline comments created?

A

Multiline comments begin with a slash and an asterisk (/*) and end with the same combination reversed (*/).

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

Is C# case sensitive?

A

C# is case-sensitive. Variables named myVar and MyVar are two different variables.

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

What are top-level statements?

A

You can create simple applications without defining a namespace, declaring a class, and defining a Main method.

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

What are four ways of creating a string that says “Hello World”?

A
string s1 = new string("Hello, World!");
string s2 = "Hello, World!";
var s3 = "Hello, World!";
string s4 = new("Hello, World!");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can the var keyword be used with members of types.

A

the var keyword cannot be used with members of types.

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

What is scope?

A

The scope of a variable is the region of code from which the variable can be accessed.

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

What is the scope of a field?

A

The entire class.

A field (also known as a member variable) of a class is in scope for as long as its containing class is in scope.

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

What is the scope of a local variable?

A

A local variable is in scope only within it’s block.

A local variable is in scope until a closing brace indicates the end of the block statement or method in which it was declared.

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

What is the scope of a variable in a loop?

A

The loop itself.

A local variable that is declared in a for, while, or similar statement is in scope in the body of that loop.

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

Can a variable with the same name be declared more than once within a single scope?

A

No.

However, bear in mind that local variables with the same name can’t be declared twice in the same scope.

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

How do you declare a variable that should never change?

A

Use the ‘const’ keyword.

For values that never change, you can define a constant. For constant values, you can use the const keyword.

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

What are three characteristics of constants?

A
  • They must be initialized when they are declared.
  • You can’t initialize a constant with a value taken from a variable.
  • Constants are always implicitly static.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If a variable in different instances of a class or method can have different values, but once that variable is decided it will never change, should it be made into a constant?

A

No, make it readonly instead

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

Can you place top-level statements in any file?

A

No.

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

Can a value type such as int, float, or double be null?

A

Not normally. The ‘?’ operator has to used to make a value type capable of being null.

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

Can reference types, such as a string, be made null.

A

Yes.

A special effort is needed to make them not be nullable. This is the purpose of Nullable Reference Types.

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

Why can reference types with a null value be problematic?

A

If you try to perform an operation on a variable that has a null value, it will most often throw an exception.

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

What is the purpose of “nullable reference types”?

A

To reduce NullReferenceExceptions being thrown.

“Nullable reference types” has to be turned on and once it is the compiler will create an error if a reference value is null.

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

What is the null-conditional operator?

A

It is a way to check a reference for being null before invoking a method on it (If you do, and the reference variable is null, you will get a NullReferenceException).

The null-conditional operator is: .? (A period and a question mark.)

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

What is the coalescing operator?

A

It’s a way of defining a default return type is a reference variable is null. This operator is two question marks: ??

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

When you declare an ‘int’ in C#, what are you actually doing?

A

You are actually declaring a ‘System.Int32”.

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

What are the built-in value types?

A
  • integers
  • floating-point numbers (float, double, decimal)
  • characters
  • Booleans
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What are the 8-bit integer types?

A
  • byte (can only have a positive value, unsigned)
  • sbyte (can have negative and positive values)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What are the 16-bit integer types?

A
  • short
  • ushort (unsigned)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What are the 32-bit integer types?

A
  • int
  • uint (unsigned)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What are the 64-bit integer types?

A
  • long
  • ulong (unsigned)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

How do check an integer types maximum and minimum values?

A

Use one these properties:

  • MaxValue
  • MinValue

Example:

Console.WriteLine(int.MaxValue);

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

What are digit separators?

A

They are a way to make numbers easier to read. The ‘_’ operator.

In C# the number

10000000000

is equvilante to

10_000_000_000

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

What is a floating-point type?

A

It is a number that that has decimals. Unlike integers which cannot support decimals.

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

What are the four types of floating-point numbers?

A
  • System.Half
  • float
  • double
  • decimal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Which floating point type can be used with money transactions?

A

The decimal type.

35
Q

What values can a Boolean type take?

A

A boolean is a value type that has only two possible values:

  • true
  • false
36
Q

What is a character type?

A

A character type is

  • a value type
  • is create with the ‘char’ keyword
  • the value is assigned using single quotes.

Example:

char c = ‘c’;

If you used double quotes you would have a string.

string s = “s”;

37
Q

How many built-in reference types are there?

A

Two

  • object
  • string
38
Q

What are the four general-purpose methods created by the object type?

A
  • Equals
  • GetHashCode
  • GetType
  • ToString
39
Q

What are the six built-in ways to controlling programming flow?

A
  • The ‘if’ statement
  • The ‘switch’ statement
  • The ‘for’ loop
  • The ‘while’ loop
  • The ‘do-while’ loop
  • The ‘foreach’ loop
40
Q

What’s the difference in the programming flow created by the ‘if’ and ‘switch’ statement versus the loop statements, ‘for’, ‘while’, ‘do-while’, ‘foreach’.

A

‘if’ and ‘switch’ will branch you program.

The loops will repeat statements until a condition is met.

41
Q

What are the three keywords associated with the ‘if’ statement?

A
  • if
  • else if
  • else
42
Q

What is pattern matching?

A

A somewhat confusing term often not specifically defined in books, including the one these flashcards are based on.

Pattern matching can be thougth of as ways (the different ways of doing this is the pattern) to test data for certain conditions.

43
Q

How do you use the const pattern with the ‘is’ statement? (Using an ‘if’ statement).

A

This compares one item with a constant to determine if they are similar.

44
Q

How do you use the type pattern with the ‘is’ statement? (Using an ‘if’ statement).

A

Here an object is being asked if it is a certain Type, if it is it evaluates to true.

45
Q

Which is a switch statement?

A

Performs the same function as an ‘if’ statement, but the switch has more flexability. Also, you do not need to use as many curly braces.

46
Q

What are the six important keywords and operators associated with switch statements.

A
  • switch
  • case
  • default
  • when
  • break
  • : (the colon)
47
Q

Can switch statements make use of pattern matching?

A

Yes.

48
Q

How do you use the constant pattern with the switch statement?

A

This is so easy that giving it a name makes it seem harder.

Basically, if the object has the value of the constant, then that is the case that is chosen.

49
Q

How do you use the type pattern with the switch statement?

A

If the object is the same type as the type being asked for, that case will be selected.

50
Q

How is the relational pattern used with the switch statement?

A

The ‘when’ keyword used with a type pattern is the relational pattern.

First, this pattern looks for a specific type, but also constrains that type to be a certain value or range of values.

51
Q

Are the switch statement and switch expression the same thing?

A

No, they are two different ways of using the switch statement, but they do perform a similar function.

The biggest obvious difference is that the switch statement uses the ‘case’ keyword, while the switch expression does not, but uses “ => “ instead.

52
Q
A
53
Q

With regard to switch expressions, what are the pattern combinators.

A

They are the ‘and’, ‘or’, and ‘not’ keywords.

You use them just as you would to create a compound conditional if statement

54
Q

What are the four loop types?

A

for, while, do - while, and foreach.

55
Q

What are the three parts of a for loop?

A

The initializer, the condition, and the iterator.

56
Q

What is a ‘while’ loop?

A

A while loop continues while a specified condition is true. When the condition is changed to false the loop stops.

Basic format:

while(condition is true)

statement(s);

57
Q

What is the do-while loop?

A

The do-while loop is an upside down version of the do loop.

It has this format:

do

{

statement(s);

}while(condition)

58
Q

When would a ‘do-while loop’ be used instead of a ‘while loop’?

A

The ‘while loop’ is the more normal of the two. If you need to be sure the code in the loop is executed at least once, then use the ‘do-while loop’.

With a ‘while loop’ it is possible the condition is false before the loop is reached, in that case the loop would not be executed.

59
Q

What is a ‘foreach loop’?

A

A ‘foreach loop’ will allow you to step throw a collection of objects (basically a list of items) then do something with each item in that collection.

60
Q

List the keywords associated with a foreach loop.

A
  • foreach
  • in
61
Q

Other than reaching the conditions set in a loop, what are three other ways of stopping an iteration.

A
  • break (this will end the loop)
  • coninue (this will not end the loop but go back to the top of the loop)
  • return (this will exit a method that is in the loop and exit the loop)
62
Q

What are the purpose of namespaces?

A

To prevent conflicts between classes that might have the same name. The namespace that the class is associated with is part of that class full name.

63
Q

Can a string type be changed?

A

No, strings are immutable.

However, this is mostly not noticeable, especially for small strings, because if a string is changed, .net will delete the old one and create a new without you even realizing it. If the strings become very large though, this will become probamatic.

64
Q

What is string concatenation?

A

It is when two strings are added together?

65
Q

What is StringBuilder?

A

StringBuilder is an alternative to the string type. It allows you to work with strings without creating an new object every time the string is changed.

66
Q

What is string interpolation?

A

It allows you to insert a variable into the body of the string.

67
Q

What are the important operators associated with string interpolation?

A
  • $ (The dollar sign)
  • { } (the two squiggly brackets)
68
Q

What is a verbatim string? What is the verbatim symbol?

A

The verbatim symbol is @.

A verbatim string returns the string just as it is between the two quotes, “”.

69
Q

What are the two types of comments?

A

The single lined comment,

And the multiple line comment.

70
Q

What are the operators for the single and multiple line comments?

A

// (Two slashes for the single line comment)

/* a bunc of words */ (Start the comment with a slash/asterisk, add your comments, then end with asterisk/slash).

71
Q

What is XML documentation?

A

It’s a way to document and clutter-up your code while you are in the very act of writing the code. It uses three of the slashes ( /// ).

What is nice about xml documentation are that these comments will show up in intellisense.

72
Q

What is an identifier?

A

It’s the name you create for a variable, method, class, or other objects.

73
Q

Are indentifiers cases sensitive?

A

Yes.

YourName

and

yourName

are not the same to C#.

74
Q

What’s the rule for beginning an identifier?

A

An identifier (also called a name) must start with a letter or underscore.

So yourName and _yourName are both valid names

But 2yourName and @yourName are not valid

75
Q

What is the rule concerning identifiers and keywords?

A

You can’t use keywords as identifiers.

The exception being if you prefex the keyword with the @ symbol

76
Q

What should your identifiers reflect?

A

They should reflect the underlining purpose of the identifier?

77
Q

Are there many conventions (general agreed on ways of doing things) in c# which does not hinder the running of the code if not follow, but if not followed will make it harder for others to read your code.

A

Yes, of course there are. More rules.

But they are rules designed to make the code easier to read, which is a good thing.

78
Q

What is Pascal casing and when should you use it for your names?

A

Pascal casing is the capitalizing the first letter of each word that makes up a name.

79
Q

When should Pascal casing be used for a name?

A

The major parts of your code should use Pascal casing, such as namespaces, classes, methods, etc.

80
Q

Should the _ (underscore) be used to join words in an identifier.

A

No.

Use Pascal casing instead.

81
Q

What is camel casing and when should it be used?

A

Camel casing is a bit like Pascal casing except the first letter of the indentifier is lower case.

It should be used for:

  • private fields
  • parameters passed to methods
  • Items that otherwise have the same name
82
Q

When should you use a property?

A

If the item should look and behave like a variable.

83
Q

Should a field be public or private.

A

Almost always private.