2. C# Language Basics || C# 10 Flashcards
What is a statement block?
Series of statement surrounded by a pair of braces (all of this is a method)
int feetToInches (int feet) { return 12 * feet; }
What is an “assembly”?
Assemblies are the fundamental units of deployment, version control, reuse, activation scoping, and security permissions for .NET-based applications. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (.dll) files, and are the building blocks of .NET applications. They provide the common language runtime with the information it needs to be aware of type implementations.
In .NET and .NET Framework, you can build an assembly from one or more source code files. In .NET Framework, assemblies can contain one or more modules. This way, larger projects can be planned so that several developers can work on separate source code files or modules, which are combined to create a single assembly.
Assemblies have the following properties:
Assemblies are implemented as .exe or .dll files. For libraries that target .NET Framework, you can share assemblies between applications by putting them in the global assembly cache (GAC). You must strong-name assemblies before you can include them in the GAC. Assemblies are only loaded into memory if they're required. If they aren't used, they aren't loaded. Therefore, assemblies can be an efficient way to manage resources in larger projects. You can programmatically obtain information about an assembly by using reflection. You can load an assembly just to inspect it by using the MetadataLoadContext class on .NET and .NET Framework. MetadataLoadContext replaces the Assembly.ReflectionOnlyLoad methods.
Name some predefined and custom types. What is the main difference between them?
Predefined: string, int, bool;
Custom: Person, Salary, Payment;
Predefined types dont need to be instantiated like custom does:
int three = 3; Person jack = new Person(“Jack”);
How would you describe static class?
Static members (data and function members) dont operate on the instance of the type e.g. Console.WriteLine()
and not var x = new Console
. Methods of the static class are available through class name (Console) and not instance name (x).
What is a implicit conversion?
short -> int -> long;
Implicit conversion happens automatically: int -> long
Allowed when both are true:
1. The compiler can garuantee that they will always succeed;
2. No information is lost in conversion;
What is an explicit conversion?
Explicit conversion require a cast and this type of conversion is required when one of the following is true:
1. The compiler cannot garuantee that they will always succeed
2. Information might be lost during conversion
What are the types categories in C#?
• Value types
• Reference types
• Generic type parameters
• Pointer types
Which types lay under the value type? (Name more than 3)
Value types comprise most built-in types (all numeric types, char and bool types) as well as custom struct and enum types
Which types lay under reference type?
All classes, array, delegate, interface types. Includes predefined string type as well
What is the fundamental difference between value and reference types?
The way they are handled in memory. Value type objects are always copied or created, but assigning a reference type the reference is copied not the object instance.
What are nullable variables?
Variables which references points to no objects. Thats why we get NullReferenceException
. This also means that value type cannot have a null as value if not declared otherwise (?)
What is the difference between i++ and ++i?
Both are adding 1 to the variable, but the difference is whether we want variable’s value before or after the increment:
console.writeline(x++);
outputs 0; value is now 1;
console.writeline(++x);
outputs 1; value is 1;
What is “short-circuiting”? Where it is encountered?
It is encountered when using && and || operators in a condition (comparing to & and |). In short if the first part of the condition is not true the following part is not evaluated, therefore if the first statement is false this statement wont throw a NullReferenceException
If ( sb != null && sb.Length >0)…
What is a ternary operator?
Ternary operator ( condotional operator or shorthand if statement) - has the form “question ? AnswerA : AnswerB”
Give an example of verbatim string
var string = @“\\server\files\heloworld.cs”;
It can also support multiple line strings
Give an example of interpolated string
var sentence = $”Hi, my name is {name}!”;
Can we combine verbatim and interpolated strings? How?
Yes we can. Interpolated string symbol must go before verbatim like so:var string = $@“mixed string {x}”;
What is an array?
Array represents a fixed number of variables (elements) of a particular type. The elements in array are always stored in a contiguous block of memory providing highly efficient access
What’s an array initialisation expression?
Expression which lets you declare and populate an array in a single step:char[] vowels = {‘a’, ‘e’, ‘i‘, ‘o ‘, ‘u’};
What are the other types of array?
Multidimensional arrays: rectangular and jagged arrays.
Rectangular: represent n-dimentional block of memory.
Jagged arrays: arrays of arrays
What is a variable?
A variable represents a storage location that has a modifiable value. A variable can be:
a local variable,
parameter (value, ref, out, or in),
field (instance or static) or
array element
What are the stack and the heap? What are the differences between them?
Stack: block of memory for storing local variables and parameters. The stack logically grows and shrinks as a method or function is entered and exited. If the value type is not a local variable or parameter it lives wherever the variable was created.
Heap: memory in which objects (reference-type instances) reside. Whenever new object is created it is allocated on the heap and the reference to that object is returned. During a programs execution the heap begins filling up as new objects are created. The runtime has a garbage collector that periodically deallocated objects from the heap so the program does not run out of memory. An object is eligible for deallocation as soon as it’s not referenced by anything that’s itself “alive”
What is a definite assignment?
Definite assignment is a concept in C# that ensures that all local variables are initialized with a value before they are used in the program.
When you declare a variable in C#, the compiler will ensure that you assign a value to it before you try to use it. If you attempt to use an uninitialized variable, you will get a compile-time error
- local variables must be assigned a value before they can be read;
- function arguments must be supplied when a method is called
- all other variables are automatically initialised by the runtime
What are the default values for most common types? How to get them?
- Reference type: null
- Numerics and enum types: 0
- Char type: ‘\0’
- Bool type: false
To get them we can use “default” as:Default(decimal) or decimal smth = default;