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.
What are exceptions?
A feature of many modern object oriented languages it is a mechanism for handling errors. Classes can throw exceptions (objects) that stop the normal execution of code and instead get passed up the calling hierarchy until they are either handled by a try catch finally block, or reach the top of the calling hierarchy.
How do you pass an object by reference?
Use either ref or out keyword.
What is the difference between ref and out?
Both keywords indicate passing a parameter by reference. Ref is to be used for passing value in and out, whereas out is just for method to set value.
What is the affect of modifying a value type within a method where it has been passed by reference?
Within the method the parameter is the sae memory location as outside so any modifications to it are reflected outside too.
What happens when within a method you assign a new object to a reference parameter where it is a reference type?
As the parameter is a effectively a pointer to a pointer, assigning a new object to it causes the variable outside of the method to point to the new object.
Which access modifiers should fields generally be given?
Private or protected. Access to fields should be controlled via properties, methods, and indexers.
Declare a field called size of type int with private accessibility
private int size;
What is the order in which fields and constructors are initialized/called?
Fields are initialized first. Constructors are then called beginning with the base class.
What is a default parameter? How are they useful?
Default parameters are specified with the optional keyword. They can be used in some cases instead of overloading a method. We can omit optional parameter from the method call
What keyword must be present in the definition of a method in the base class in order for a derived class to override the method?
The virtual keyword is specified in the base class. The derived method is specified with the overrides keyword.
What is this code of the ColorCoOrds class which derives from CoOrds class and what does it do?
public ColorCoOrds(int x, int y) : base (x, y) { color = System.Drawing.Color.Red; }
The ColourCoOrds class calls the constructor on its base class which accepts two integer parameters before it’s own constructor code gets executed.
What are optional/default parameters?
Optional parameters are parameters that can be omitted from method calls. In the method declaration optional parameters are declared by assigning a default value in method signature. Default values must be able to be determined at run-time, e.g. constants, null values.
Where must optional/default parameters be specified in a method declaration?
Optional parameters must be specified after all required parameters
How does intellisense indicate an optional parameter?
Intellisense displays optional parameters in [square brackets] along with their default value.
Declare a void method countdown which takes in an optional int parameter with a default of 10
public void Countdown(int timer = 10)
What is a named argument?
A named argument is where the client has specified the name of the parameter in the method call.
Use named arguments to call this method:
public decimal CalculateBMI(int height, int weight) {…}
CalculateBMI(weight: 76, height: 180);
Why are named and optional/default arguments useful together?
In methods where there are many parameters the use of optional/default and named arguments allow method calls without specifying values for every parameter. Especially useful with COM interfaces such as Microsoft Office