C# Interview Questions on Fields


What are the 2 broad classifications of fields in C#?
1. Instance fields
2. Static fields

What are instance fields in C#?
Instance fields are specific to an instance of a type. If you have a class T, with an instance field F, you can create two objects of type T, and modify the value of F in each object without affecting the value in the other object.

What is a static field?
A static field belongs to the class itself, and is shared among all instances of that class. Changes made from instance A will be visible immediately to instances B and C if they access the field.

Will the following code compile?

using System;
class Area
{
   public static double PI = 3.14;
}
class MainClass
{
   public static void Main()
   {
      Area A = new Area();
      Console.WriteLine(A.PI);
   }
}
No, a compile time error will be generated stating "Static member 'Area.PI' cannot be accessed with an instance reference; qualify it with a type name instead". This is because PI is a static field. Static fields can only be accessed using the name of the class and not the instance of the class. The above sample program is rewritten as shown below.

using System;
class Area
{
   public static double PI = 3.14;
}
class MainClass
{
   public static void Main()
   {
      Console.WriteLine(Area.PI);
   }
}

Can you declare a field readonly?
Yes, a field can be declared readonly. A read-only field can only be assigned a value during initialization or in a constructor. An example is shown below.

using System;
class Area
{
   public readonly double PI = 3.14;
}
class MainClass
{
   public static void Main()
   {
      Area A = new Area();
      Console.WriteLine(A.PI);
   }
}

Will the following code compile?

using System;
class Area
{
   public readonly double PI = 3.14;
}
class MainClass
{
   public static void Main()
   {
      Area A = new Area();
      A.PI = 3.15;
      Console.WriteLine(A.PI);
   }
}

No, PI is readonly. You can only read the value of PI in the Main() method. You cannot assign any value to PI.

What is wrong with the sample program below?

using System;
class Area
{
   public const double PI = 3.14;
   static Area()
   {
      Area.PI = 3.15;
   }
}
class MainClass
{
   public static void Main()
   {
      Console.WriteLine(Area.PI);
   }
}
You cannot assign a value to the constant PI field.

What is the difference between a constant and a static readonly field?
A static readonly field is very similar to a constant, except that the C# compiler does not have access to the value of a static read-only field at compile time, only at run time.

4 comments:

  1. static members cannot access with object can anybody give reason why?

    ReplyDelete
    Replies
    1. Because static members belongs to the class not to specific instance of that class, so we access them using the class name itself.

      Delete
  2. Because static members instantiated before the main method of the class calls.
    static member have the property to be accessed using class name and give compile time error if accessed using instance name.

    ReplyDelete
  3. Static members are class specific and hence the value of the static member will be common for all instance of the class. Hnece we can accessed only by class name or directly static member

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us