C# Flashcards
What are the 2 floating point types in C#?
Float - 32 bits. Literal suffixes with f, e.g. 3.5f
Double - 64 bits. By defaultreal numeric literal treated as double. To force integer to be treated as double literal suffix with d.
What is a floating point number?
A floating point number is a method of representing an approximation of a real number that can support a wide range of values. It is represented by a fixed number of digits and a scaled using an exponent. They can be stored efficiently in binary allowing great range but less precision.
How do you declare an array of 10 integers named numbers?
int[] numbers = new int[10];
What is the property of strings that mean that they cannot be modified?
Immutable. Strings objects cannot be modified after they have been created. Concatenation strings results in a new string object being created.
What abstract base type of all arrays types?
Array. Arrays are objects in C#, unlike in C and C++ where they are just addressable contiguous areas of memory. It has several useful properties and methods defined on it.
What is a delegate?
A delegate is a type-safe function pointer. The delegate type defines a method signature. Delegates do that type can reference methods with a matching signature.
Define a delegate Add, that takes in two integer values as parameters
private delegate int add(int a, int b);
How can delegates be used?
Callback methods
Event handlers
Anonymous methods
What are events?
Events enable a class or object to notify other classes or objects when something of interest happens. The event pattern is closely related to the Observer pattern.
Name all 4 access modifiers
private - restricted to containing type
public - access not restricted
protected - restricted to containing type or types that derive from it
internal - restricted to current assembly
What is a constant?
Constants are immutable types whose value is known at compile-time. Only the C# built-in types can be declared as const.
Declare a constant for the value of pie
const decimal pie = 3.14;
What does the static keyword do and how can it be used?
The static keyword can be used to declare a member static. It can be used on classes, fields, methods, properties, operators, events, and constructors.
What is a static class and what are its properties?
Static classes are classes defined with the static keyword. Static classes cannot be instantiated. All of its members must be static also.
What is a popular example of a static class in the .NET framework?
The Math class. It provides utility methods for performing mathematical operations.
What design pattern is commonly used to ensure that only a single instance of a class can be created?
Singleton. There are a few ways to implement this, each of which has trade-offs. It involves making the constructor private so that instantiation can be controlled via a static method.
What is a field?
A field is variable of any type that is declared directly in a class or struct. They are members of their containing type.
How do you prevent a class from being inherited?
The sealed keyword prohibits a class from being inherited
What is the execution entry point for a C# console application?
The Main method
How do you initiate a string without escaping each backslash?
You put an @ sign in front of the double-quoted string.
String ex = @”This has a carriage return\r\n”;
What is the difference between a struct and a class?
Structs cannot be inherited. Structs are passed by value and not by reference. Structs are stored on the stack not the heap. The result is better performance with Structs.
What are namespaces, and how they are used?
Namespaces are used to organize classes within the .NET Framework. They dictate the logical structure of the code. They are analogous to Java packages, with the key difference being Java packages define the physical layout of source files (directory structure) while .NET namespaces do not. However, many developers follow this approach and organize their C# source files in directories that correlate with namespaces. The .NET Framework has namespaces defined for its many classes, such as System.Xml–these are utilized via the using statement. Namespaces are assigned to classes via the namespace keyword.
What is garbage collection?
Garbage collection is a feature of most modern object oriented languages. It is a feature of the runtime that disposes of objects that are no longer referenced
What does type safe mean? Why is C# type safe where JavaScript for example isn’t?
Type safety relates to how a language enforces constraints on the kinds of operations that can be performed on a type such that only valid operations can be performed. For example, the method quack() can’t be called on a swan object that does not implement this method. JavaScript does not enforce this and would not generate an error until runtime.