User FAQs

If you have a question, please send it to us.


1) Can you describe the four different types of inheritance ?
Single Inheritance - contains one base class and one derived class.
Hierarchical Inheritance - contains one base class and multiple derived classes with the same base class.
Multi-level Inheritance - contains a class derived from another derived class.
Multiple Inheritance - (not supported in C#) contains a class derived from multiple base classes.


2) Can you explain the difference between passing by value and passing by reference ?
When you pass a Value data type by value "a copy" is passed in and the original cannot be changed.
When you pass a Value data type by reference "a copy of the address" is passed in allowing the original to be changed.
When you pass a Reference data type by value "a copy of the address" is passed in allowing the original to be changed.
When you pass a Reference data type by reference "the address" is passed in allowing the original to be changed.

public Method( 
    int ValueType1,
    ref int ValueType2,
    string ReferenceType1,
    ref string ReferenceType2)

    // ValueType1 - original value cannot be changed
    // ValueType2 - original value can be changed
    // ReferenceType1 - original value can be changed
    // ReferenceType2 - original value can be changed
}

3) What is the default when you are passing in arguments, passing by value or passing by reference ?
The default is passing by value.

public Method(int Number1, string Text2) 

4) How would you describe the default Access Modifier ?
The default access modifier is the least restrictive based on the current scope.


5) Can you describe the five different levels of accessibility ?
public - everywhere
protected internal - inside the class and from any derived class in any assembly and from anywhere in that assembly (base classes)
internal - anywhere in that assembly
protected - inside the class and from any derived class in any assembly (base classes)
private - inside the class


6) What is a Class ?
A class is the definition of an object.
A class can contain fields, methods, properties, constructors, finalizer, destructor, indexers.
A class is a reference type.
Classes can be 'public', 'internal'.
The default access modifier for a class is 'internal'.

internal class MyClass 
{ }

7) What is a Partial Class ?
A partial class splits a class's definition into multiple chunks, allowing you to split a class into separate source files.


8) What type of Access Modifiers can you have on Class members.
Class members can be 'public', 'protected internal', 'internal', 'protected', 'private'.
The default access modifier for a class member is 'private'.


9) Can a Class inherit from a Interface ?
Yes. Although the correct term is implement rather than inherit.
A class can implement one or more interfaces.

class MyClass : MyInterface, MyInterface2, MyInterface3 
{ }

There must be Explicit Interface Implementation when you have two interfaces with the same methods.


10) Can a Class inherit from a Class ?
Yes. However you cannot inherit from multiple classes at the same time.

class MyDerived : MyClass 
{ }

You can however chain together classes that inherit off one another.

class MyClass_A 
{ }

class MyClass_B : MyClass_A
{ }

class MyClass_C : MyClass_B
{ }

11) Can a Class inherit from a Class and an Interface ?
Yes. Although to be precise the class inherits from a class and at the same time can implement one or more interfaces.
The class must come first, followed by the interface(s).

class MyDerived : MyClass, MyInterface1, MyInterface2 
{ }

12) If you have an Internal Class should you make the members 'internal' or 'public' ?
A public member of an internal class is effectively internal because the class only has internal scope.
The members should be declared 'public' in case you want to change the class later to be public instead of internal.


13) Can you describe a situation when you need to have a 'public' method inside an Internal Class ?
Yes. This is necessary when an internal class implements an internal interface.
Interface members are always 'public' by definition and therefore the class must implement that member using a public access modifier.

internal interface MyInterface 
{
   void MyMethod()
}
internal class MyClass : MyInterface
{
   public void MyMethod()
   { }
}

14) What is a Nested Class ?
A nested class is a class declaration that occurs inside another class.
Nested classes can be 'public', 'protected internal', 'internal', 'protected', 'private'.
The default access modifier for a nested class is 'private'.

class MyClass 
{
   private class MyNested
   { }
}

15) What is a Class Method ?
A class method is a procedure or function defined inside a class that contains statements.

class MyClass 
{
   void MyMethod()
   { }
   int MyMethod2()
   { }
}

16) What is the difference between ref and out parameters ?
ref - An argument passed as ref must be initialised before it is passed into a method.
out - An argument passed as out does not need to be initialised beforehand.

public void MyMethod(ref string myText1, 
                     out string myText2)
{ }

17) Describe the 'return' statement and explain where it can be used ?
return - terminates execution of the method in which it appears and returns control to the calling method.


18) Can you describe Method Overloading ?
This is when you create another method with the same name but with a different signature.
The compiler determines which method to invoke at run-time.
There are three ways a method can be overloaded:
*) By changing the number of parameters.
*) By changing the order of the parameters.
*) By changing the data types of the parameters.


19) What is a Class Property ?
A property provides a way of exposing an 'internal data element' so it can be accessed from outside the class.
You create a property by providing set and get property accessors.
The get accessor is used to return a value.
The set accessor is used to assign a new value.

class MyClass 
{
   private string _TextField;

   public string Property_Name
   {
      get { return _TextField; }
      set { _TextField = value; }
   }
}

20) What is an Auto Implemented Property ?
This provides a shorthand for the above by implicitly creating a private field.

public string Property_Name { get; set; } 

21) What does the abstract modifier do ?
This can only be applied to methods and classes.
When applied to a method it creates an abstract method that HAS NO implementation.
An abstract method can only appear in an abstract class.


22) What is an Abstract Class ?
This class cannot be instantiated and can only be used as a base class.
Abstract classes can be 'public', 'internal'.
You must provide at least one abstract method in an abstract class.
These classes can provide implementation.

public abstract class MyAbstract 
{
   public abstract void MyMethod()
   internal abstract void MyMethod2()
   protected abstract void MyMethod3()
}

23) What type of Access Modifiers can you have on Abstract Class members ?
Abstract class members can be 'public', 'protected internal', 'internal', 'protected', 'private'.
The default access modifier for an abstract class method is 'public'.
An abstract method is by definition also a virtual method.


24) What is the difference between an Abstract Class and an Interface ?
An abstract class can have private and protected methods but an interface can only have public.
An abstract class can contain methods with implementation but an interface cannot contain any implementation.
An abstract class can have a constructor. An interface cannot have a constructor.
Classes can implement multiple interfaces but can only implement one abstract class.
Methods in an abstract class can be abstract or concrete. Methods in an interface are all abstract by definition.
A class extending an abstract class may or may not implement any of its methods. All methods of an interface have to be implemented when a class implements that interface.


25) Can you describe a situation when you need an 'internal' method inside an abstract class ?
If you want to prevent external inheritance while retaining visibility.


26) What is the difference between a Base Class and a Derived Class ?
A base class is a class declaration that is used as a basis for a second derived class.
If you want to extend the functionality or customise the functionality of an existing class you can derive another class from it.
A derived class inherits both structure and behaviour.


27) What does the static modifier do ?
This can be applied to classes and methods.
It means you can call this method without instantiating the object.
A static class can only contain static members.
You cannot have a virtual static member.

static class MyClass 
{
   static void MyMethod()
}

28) Can a Class inherit from a Static Class ?
No.


29) What does the virtual modifier do ?
This is applied to methods.
It means you HAVE THE OPTION TO override this method in a derived class.

class MyClass 
{
   public virtual void MyMethod()
   {
      MessageBox.Show("MyClass - MyMethod");
   }
}

30) Can a private, virtual method be overridden ?
A private virtual method cannot be overridden as it can't be accessed outside the class.


31) What does the override modifier do ?
It means you have REPLACED the method in the derived class.
This changes the behaviour of the derived class.
You can override virtual methods in a normal class.
You can override abstract methods in an abstract class.
The method signature must be identical.

class MyClass 
{
   virtual void MyMethod()
   { MessageBox.Show("MyClass - MyMethod"); }
}

class MyDerived : MyClass
{
   override void MyMethod()
   { MessageBox.Show("MyDerived - MyMethod"); }
}

or

abstract class MyAbstractClass 
{
   abstract void MyMethod()
   { MessageBox.Show("MyAbstractClass - MyMethod"); }
}

class MyDerived : MyAbstractClass
{
   override void MyMethod()
   { MessageBox.Show("MyDerived - MyMethod"); }
}

What is displayed when this code runs.
Both methods return MyDerived because this method has been overridden.

MyClass a = new MyDerived(); 
MyDerived b = new MyDerived();
a.MyMethod(); // MyDerived - MyMethod
b.MyMethod(); // MyDerived - MyMethod

32) What does the new modifier do ?
This can only be applied to methods.
It means you have CREATED a method with the same name in a derived class.
This is the DEFAULT when your derived class has members with the same name as the base class.

class MyClass 
{
   virtual void MyMethod()
   { MessageBox.Show("MyClass - MyMethod"); }
}
class MyDerived : MyClass
{
   new void MyMethod()
   { MessageBox.Show("MyDerived - MyMethod"); }
}

What is displayed when this code runs.
Variable "a" is of type MyClass but variable "b" is of type MyDerived.

MyClass a = new MyDerived(); 
MyDerived b = new MyDerived();
a.MyMethod(); // MyClass - MyMethod
b.MyMethod(); // MyDerived - MyMethod

33) What is the difference between the 'new' modifier and the 'override' modifier ?
The 'override' modifier will create a different method in the derived class.
The 'new' modifier will replace the base class member.


34) What does the sealed modifier do ?
This is applied to classes and methods.
It means you cannot override the base class method.
A sealed class cannot be inherited from.
A sealed method cannot be overridden in a derived class.

class MyClass 
{
   sealed void MyMethod()
}

35) What does the volatile modifier do ?
It means that its value can be changed by another thread.
This modifier tells the compiler that this field could be accessed on multiple threads.
When the code is compiled, the compiler will make various optimisations on the assumption that variables will only be accessed by one thread at a time.
Using this modifier ensures that one thread retrieves the most up-to-date value written by another thread.
Writing non-blocking or lock free multithreaded code is very complicated.

class MyClass 
{
   volatile void MyMethod()
}

36) What does the readonly modifier do and how is different to 'const' ?
This is a modifier that can be used on fields.
Therefore readonly fields can have different values depending on the constructor used.

class MyClass 
{
   public readonly int _NumberField = 5;
}

37) What is the difference between the 'readonly' modifier and the 'const' modifier ?
A 'readonly' variable can be initialised at run-time or in the class's constructor.
A 'constant' variable must be initialised at compile-time in the declaration.


38) What is a Constructor ?
A constructor is a member function in a class that has the same name as the class.
This method is automatically invoked when an object is created.
It can be used to initialise the class members.
They can be overloaded.
They do not return a value.
Having a class constructor is optional.

class MyClass 
{
   void MyClass() // constructor
   { }
}

39) Can you overload the default Constructor ?
Yes. You can leave the parameterless one as well if you want to.

class MyClass 
{
   void MyClass() // constructor
   { }
   void MyClass(string myText) // constructor
   { }
}

40) Can you call one constructor from another constructor in the same object ?
Yes. One constructor can invoke another constructor by using the this keyword.

class MyClass 
{
   void MyClass() // constructor
      : this("default text") // invokes the constructor below
   { }
   void MyClass(string myText) // constructor
   { }
}

41) Can you describe when you would use a Private Constructor ?
Private constructors are used to prevent creating instances of a class that have no instance fields or methods.
It ensures the object can only be created by a member of the class.

class MyClass 
{
   private MyClass() // constructor
   { }
}

42) Can a Class have a Static Constructor ?
Yes. A non static class can have a static constructor. It can be used to initialize static data.
It is called automatically before any static members are referenced.
It is called automatically before the first instance is created.

class MyClass 
{
   static MyClass() // constructor
   { }
}

43) What is the base keyword used for ?
The base keyword is used to access members of the base class from within a derived class.
The base class can only be accessed in a constructor, an instance method or an instance property accessor.
If the derived class overrides a base class method then the base keyword can be used to call the base class method.
You can also use the base keyword to specify which base class constructor should be called on the derived class constructor.


44) Is it possible for a class to inherit the constructor of its base class ?
No.


45) If a base class has an overloaded constructor and a derived class also has an overloaded constructor how can you call the base class constructor ?

public class MyClass 
{
   public MyClass(string MyText) // constructor
   {
   }
}
public class MyDerived : MyClass
{
   public MyDerived(string MyText, int myValue)
      : base(MyText)
   {
   }
}

46) What is a Destructor ?
A destructor implicitly calls the System.Object.Finalize method on the base class of the object.
A destructor cannot be called, it is invoked automatically.

public class MyClass 
{
   ~MyClass()
   {}
}

This is equivalent to the following:

public class MyClass 
{
   protected override void Finalize()
   {
      base.Finalize();
   }
}

47) What is a Finalizer ?
The word Finalizer refers to the System.Object.Finalize method.
Managed memory which is allocated using the new operator does not need to be explicitly released because it is released by the garbage collection process automatically.
This method can be used to clean up and destroy any unmanaged memory resources.
However there are two shortcomings with this method:
*) This method (if it runs at all) is called at some undetermined time in the future which means the resource is not cleaned up immediately.
*) This method executes on a different thread and therefore the memory is not actually released until the automatic garbage collection runs again.


48) What is the difference between Dispose() and Finalize() methods?
Dispose() is used when an object needs to dispose of unmanaged resources immediately.
Finalize() on the other hand does release unmanaged resources but this is not done immediately.


49) What is a Structure ?
A structure is the definition of an object, similar to a data record.
A structure cannot be inherited from.
A structure is a value data type (and therefore should not contain any reference data type members).
A structure can contain implementation.
A structure cannot be declared static although it can have static members.
Structures can be 'public', 'private', 'internal'.
The default access modifier for a structure is 'internal'.

internal struct MyStructure 
{
   public int _NumberField;
   internal void MyMethod();
   private bool Property_Name { get; set: }
}

A structure can contain fields, methods, properties, enumerations, delegates, events, constructors, indexers, operator methods, classes.
A structure cannot contain finalizers or destructors.


50) What type of Access Modifiers can you have on Structure members ?
Structure members can be 'public', 'private', 'internal'.
Structure members cannot be 'protected internal' or 'protected' because they do not support inheritance.
The default access modifier for a structure member is 'private'.


51) What is the difference between a Structure and a Class ?
Structures are value types (stored on the stack), but classes are reference types (stored on the heap).
Structures do not support inheritance because you cannot have 'protected' or 'protected internal' access modifiers.
Structures cannot have destructors, but classes can.
Structures cannot initialise non static fields, but classes can.


52) What are the similarities between a Structure and a Class ?
Both can contain fields, methods, properties.
Both can inherit from interfaces.
Both can have 'public', 'private' and 'internal' access modifiers.
Both can contain implementation.


53) Can a Structure inherit from an Interface ?
Yes. Although the correct term is implement rather than inherit.
A structure can implement one or more interfaces.
A structure cannot inherit from a Structure or Class.

struct MyStructure : IMyInterface, IMyInterface2 
{ }

54) Can a Structure contain a Class ?
Yes. Although this is not best practice.

struct MyStructure 
{
    public class MyClass
    {}
}

55) Can you have nested Structures ?
Yes. It is possible to declare a structure inside another structure.

struct MyStructure 
{
    public struct MyNestedStructure
    {}
}

56) Can a Structure contain a member variable that has a Structure data type.
Yes. Structures can contain member variables that have structure data types.

struct MyStructure_A 
{}

struct MyStructure_B
{
    private MyStructure_A myRecord;
}

57) Can a Structure contain 'static' fields ?
Yes. Structures can contain static fields.
Static fields can be initialised inside a structure.

struct MyStructure 
{
    static string textField;
}

58) Can a Structure contain 'static' methods ?
Yes. Structures can contain static methods.
They can be invoked by using the name of the structure.
Static members can only access other static members.


59) Describe the difference between passing a Structure to a method and passing a Class to a method ?
When a structure is passed by value "a copy" is passed in and the original cannot be changed.
When a class is passed by value "a copy of the address" is passed in allowing the original to be changed.
When a structure (or any other value type) is passed by reference the address is passed in allowing the original to be changed.


60) Can a Structure have a Constructor ?
Yes. Although it will not be the default constructor.
It is possible to include a constructor but it must have at least one parameter.
Any private members can only be initialized in a constructor.
A default constructor (with no parameters) is created automatically and therefore one cannot be defined.
It is an error to initialize an instance field in a structure constructor.

internal struct MyStructure 
{
   private long _privateField;
   public float _numberField;

   public MyStructure(string textValue)
   {
      _privateField = 100;
      _numberField = 10.5;
   }
}

61) Write code to instantiate an instance of a Structure ?
You can create an instance of a Structure using the 'new' keyword.
The first line uses the default parameterless constructor.
The second line uses a custom constructor.

MyStructure struct1 = new MyStructure(); 
MyStructure struct2 = new MyStructure(10,20);

You can also create an instance by just declaring it (without using new).
The fields will remain unassigned and the object cannot be used until all the fields are initialised.

MyStructure struct3; 
struct3.Field1 = 10;
struct3.Field2 = 20;

62) What is a Delegate ?
A delegate represents a reference to a method that has a particular signature (parameter list and return type).
Delegates give you a way of calling a method at run-time.
When you instantiate a delegate you can associate its instance with any method with a matching signature.
Delegates are used to pass methods as arguments to other methods.

public delegate int MyDelegate(int x); 

Examples include: type safe functions, pointers, callbacks.


63) Delegates with Named Methods
When you instantiate a delegate by using a named method, the method is passed as the parameter.

delegate int MyDelegate(int x); 
void DoSomething(int a)
{ return a * a; }
MyDelegate del = DoSomething;

64) Delegates with Anonymous Methods
Introduced in C# 2.0 (.NET 2.0).
An anonymous method provides a way of passing a block of code as an argument instead of creating an explicit named method.

delegate int MyDelegate (int x); 
MyDelegate del = delegate (int a)
   { return a * a; }

65) What is a Lambda Expression ?
Introduced in C# 3.0 (.NET 3.5).
A lambda expression is a more concise way of writing an anonymous function.
You specify the input parameters (if any) on the left of the lambda operator => and the statement block (or expression) on the right.

delegate int MyDelegate (int x); 
MyDelegate del = a => a * a;

These are often used to create delegates for use in LINQ queries.


66) What is the difference between a Delegate and an Event ?
An event is something that can be added or removed from an object.
When you add or create an event you are storing a delegate reference which can then be called at run-time.


67) What is an Event ?
An event is an indication that a state is about to change or that a state has changed.
An event handler is a method that is invoked using a delegate.

button.click += delegate(System.Object o, _ 
                         System.EventArgs e)
                        { MessageBox.Show("pressed"); };

68) What is a Multicast Delegate ?
A Delegate object holds a reference to a single method.
It is possible for a delegate object to hold references to multiple methods.
These delegates are called multicast or combinable delegates.


69) Can you describe some situations when you would use Delegates ?
Callback Mechanism -
Asynchronous Processing -
Multi-Casting -
Abstract and Encapsulate Methods -



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext