User FAQs

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


1) 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
}

2) 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) 

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


4) 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


5) 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 
{ }

6) 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'.


7) 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.


8) 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
{ }

9) 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 
{ }

10) 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.


11) 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()
   { }
}

12) 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
   { }
}

13) 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()
   { }
}

14) 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)
{ }

15) 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.


16) 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; }
   }
}

17) 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; } 

18) 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.


19) 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()
}

20) 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.


21) 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.


22) 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.


23) 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.


24) 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()
}

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


26) 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.
You cannot have a private virtual method.

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

27) 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"); }
}

28) 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

29) 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"); }
}

30) What is displayed when this code runs ?

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

31) 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.


32) 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()
}

33) 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()
}

34) 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;
}

35) 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.


36) 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
   { }
}

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

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

38) 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
   { }
}

39) 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
   {
   }
}

40) 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.


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


42) 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)
   {
   }
}

43) 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();
   }
}

44) 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.


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