2. Core C# Flashcards
What do statements end with?
Statements end in a semicolon (;) and can continue over multiple lines without needing a continuation character.
How do you join statements into blocks?
Statements can be joined into blocks using curly braces ({}).
How do you create a single line comment?
Single-line comments begin with two forward slash characters (//).
How are multiline comments created?
Multiline comments begin with a slash and an asterisk (/*) and end with the same combination reversed (*/).
Is C# case sensitive?
C# is case-sensitive. Variables named myVar and MyVar are two different variables.
What are top-level statements?
You can create simple applications without defining a namespace, declaring a class, and defining a Main method.
What are four ways of creating a string that says “Hello World”?
string s1 = new string("Hello, World!"); string s2 = "Hello, World!"; var s3 = "Hello, World!"; string s4 = new("Hello, World!");
Can the var keyword be used with members of types.
the var keyword cannot be used with members of types.
What is scope?
The scope of a variable is the region of code from which the variable can be accessed.
What is the scope of a field?
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.
What is the scope of a local variable?
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.
What is the scope of a variable in a loop?
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.
Can a variable with the same name be declared more than once within a single scope?
No.
However, bear in mind that local variables with the same name can’t be declared twice in the same scope.
How do you declare a variable that should never change?
Use the ‘const’ keyword.
For values that never change, you can define a constant. For constant values, you can use the const keyword.
What are three characteristics of constants?
- 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.
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?
No, make it readonly instead
Can you place top-level statements in any file?
No.
Can a value type such as int, float, or double be null?
Not normally. The ‘?’ operator has to used to make a value type capable of being null.
Can reference types, such as a string, be made null.
Yes.
A special effort is needed to make them not be nullable. This is the purpose of Nullable Reference Types.
Why can reference types with a null value be problematic?
If you try to perform an operation on a variable that has a null value, it will most often throw an exception.
What is the purpose of “nullable reference types”?
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.
What is the null-conditional operator?
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.)
What is the coalescing operator?
It’s a way of defining a default return type is a reference variable is null. This operator is two question marks: ??
When you declare an ‘int’ in C#, what are you actually doing?
You are actually declaring a ‘System.Int32”.
What are the built-in value types?
- integers
- floating-point numbers (float, double, decimal)
- characters
- Booleans
What are the 8-bit integer types?
- byte (can only have a positive value, unsigned)
- sbyte (can have negative and positive values)
What are the 16-bit integer types?
- short
- ushort (unsigned)
What are the 32-bit integer types?
- int
- uint (unsigned)
What are the 64-bit integer types?
- long
- ulong (unsigned)
How do check an integer types maximum and minimum values?
Use one these properties:
- MaxValue
- MinValue
Example:
Console.WriteLine(int.MaxValue);
What are digit separators?
They are a way to make numbers easier to read. The ‘_’ operator.
In C# the number
10000000000
is equvilante to
10_000_000_000
What is a floating-point type?
It is a number that that has decimals. Unlike integers which cannot support decimals.
What are the four types of floating-point numbers?
- System.Half
- float
- double
- decimal