Functions, Fields, and Constants Interview Questions and Answers in C#

Functions, Fields, and Constants Interview Questions and Answers in C#

In this article, I am going to discuss the most frequently asked Functions, Fields, Constants Interview Questions, and Answers in C#. Please read our previous article where we discussed the most frequently asked Constructor Interview Questions in C# with Answers. As part of this article, we are going to discuss the following Functions Interview Questions in C# with Answers.

  1. What are the constants in C#?
  2. Can you declare a class or a struct as constant?
  3. Does C# support const methods, properties, or events?
  4. Can you change the value of a constant filed after its declaration?
  5. How do you access a constant field declared in a class in C#?
  6. What is the difference between const and read-only?
  7. What are the 2 broad classifications of fields in C#?
  8. What are the instance fields in C#?
  9. What is a static field in C#?
  10. Can you declare a field read-only in C#?
  11. What is the difference between a constant and a static read-only field?
  12. Static members cannot access an object can you give the reason why?
  13. Can we Overload the main() method in C#?
  14. Why is C# main method static?
  15. What is the difference between method parameters and method arguments?
  16. Explain the difference between passing parameters by value and passing parameters by reference in C#?
  17. Can we pass value types by reference to a method?
  18. If a method’s return type is void, can you use a return keyword in the method?
  19. What is the difference between static class and class with static methods? In which case I should use either of them?
  20. What is a recursive function in c#? Give an example.
What are the constants in C#?

Constants in C# are immutable values that are known at compile-time and do not change for the life of the program. The Constants are declared using the const keyword. Constants must be initialized as they are declared. You cannot assign a value to a constant after it is declared. An example is shown below.

Fields and Constants Interview Questions in C# with Answers

Can you declare a class or a struct as constant? 

No, User-defined types including classes, structs, and arrays, cannot be const. Only the C# built-in types excluding System.Object may be declared as const. Use the read-only modifier to create a class, struct, or array that is initialized one time at runtime (for example in a constructor) and thereafter cannot be changed.

Does C# support const methods, properties, or events?

No, C# does not support const methods, properties, or events.

Can you change the value of a constant filed after its declaration? 

No, you cannot change the value of a constant filed after its declaration. In the example below, the constant field PI is always 3.14, and it cannot be changed even by the class itself. In fact, when the compiler encounters a constant identifier in the C# source code (for example, PI), it substitutes the literal value directly into the intermediate language (IL) code that it produces. Because there is no variable address associated with a constant at runtime, const fields cannot be passed by reference.

class Circle
{
    public const double PI = 3.14;
}
How do you access a constant field declared in a class in C#?

Constants are accessed as if they were static fields because the value of the constant is the same for all instances of the type. You do not use the static keyword to declare them. Expressions that are not in the class that defines the constant must use the class name, a period, and the name of the constant to access the constant. In the example below the constant field, PI can be accessed in the Main method using the class name and not the instance of the class. Trying to access a constant field using a class instance will generate a compile-time error.

class Circle
{
    public const double PI = 3.14;
}
class MainClass
{
    public static void Main()
    {
        Console.WriteLine(Circle.PI);
        Circle C = new Circle();
        // Error : PI cannot be accessed using an instance
        // Console.WriteLine(C.PI);
    }
}
What is the difference between const and read-only?

The Read-only value can be changed at runtime however const value can never change.

What are the 2 broad classifications of fields in C#?
  1. Instance fields
  2. Static fields
What are the 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 in C#? 

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 in C#?
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 the “Static member ‘Area.PI’ cannot be accessed with an instance reference; qualify it with a type name instead“. This is because the 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.

class Area
{
    public static double PI = 3.14;
}
class MainClass
{
    public static void Main()
    {
        Console.WriteLine(Area.PI);
    }
}
Can you declare a field readonly in C#? 

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.

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? 
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 the PI.

What is wrong with the sample program below? 
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 read-only 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 runtime.

Static members cannot access an object can you give the reason why?

Because static member belongs to the class not to a specific instance of that class, so we access them using the class name itself. Static members are instantiated before the main method of class calls.

Can we Overload the main() method in C#?

Yes, We can overload the main() method. A C# class can have any number of main() methods.

But to run the C# class, the class should have the main() method with signature as “public static void main(String[] args)”. If we do any modification to this signature, the compilation will be successful. But, we will get the runtime error as the main method not found.

Why is the C# main method static?

The main method is static because it is available to run when our program starts and as it is the entry point of the program it runs without creating an instance of the class. In other words, static functions exist before a class is instantiated so static is applied to the main entry point (main method).

Static methods are methods that do not require any object whenever they are called. These methods are loaded even before the class is loaded in the memory. It means that even before the object is being created, the method is already loaded into the memory.

Other than this, Main() is the door for any program. It means whenever we run a program, the compiler looks out for the Main method. If there is the main method then the content inside it is executed. The main method is the first access point for any program and has to be called automatically. Since it is static it gets loaded automatically even before the object of that class is being created and Main() does not require an object to be called

You have a component with 5 parameters and deployed to client-side now you changed your method which takes 6 parameters. How can you deploy this without affecting the client’s code?

Instead of adding the 6th parameter to the existing method, write a new overloaded method with 6 parameters.

So when the old application calls this method, the method with 5 parameters will execute and the method with 6 parameters will be used by the new application. In this way, we can provide backward compatibility to old applications.

Is the following code legal in C#?

Functions Interview Questions and Answers in C#

No, the above code does not compile. You cannot overload a method based on the return type. To overload, a method in C# either the number or type of parameters should be different. In general, the return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to.

What is the difference between method parameters and method arguments? Give an example.

In the example below FirstNumber and SecondNumber are method parameters whereas FN and SN are method arguments. The method definition specifies the names and types of any parameters that are required. When calling code calls the method, it provides concrete values called arguments for each parameter. The arguments must be compatible with the parameter type but the argument name (if any) used in the calling code does not have to be the same as the parameter named defined in the method.

What is the difference between method parameters and method arguments?
Explain the difference between passing parameters by value and passing parameters by reference in C#?

We can pass parameters to a method by value or by reference. By default, all value types are passed by value whereas all reference types are passed by reference. By default, when a value type is passed to a method, a copy is passed instead of the object itself. Therefore, changes to the argument have no effect on the original copy in the calling method. An example is shown below.

Explain the difference between passing parameters by value and passing parameters by reference in C#?

By default, reference types are passed by reference. Whenever an object of a reference type is passed to a method the reference type points to the original object, not a copy of the object. Changes made through this reference will, therefore, be reflected in the calling method. Reference types are created by using the class keyword as shown in the example below.

Functions Interview Questions in C# with Answers

Can you pass value types by reference to a method?

Yes, we can pass value types by reference to a method. An example is shown below.

Can you pass value types by reference to a method?

If a method’s return type is void, can you use a return keyword in the method?

Yes, Even though a method’s return type is void, you can use the return keyword to stop the execution of the method as shown in the example below.

If a method's return type is void, can you use a return keyword in the method?

What is the difference between static class and class with static methods? In which case I should use either of them?

If your class has only static members you never need an instance of that class so you should make the class itself static but if your class has instance members (non-static) then you have to make your class an instance class to access its instance members via instances of your class.

Static class: We cannot instantiate, inherit the static class. 

Class with full of static methods: We can instantiate this class. But logically we are not going to do anything with these instances. So practically this scenario doesn’t exist. However, it might be useful in inheriting. But practically I haven’t come across such a situation anywhere.

When it some to the static class, we use it to extend a particular object. For example, we have the accounts object. We need to add a method HasBalance(). We can do it by extension method.

However, it is very common to have classes with both static and non-static methods.

What is a recursive function in c#? Give an example. 

A recursive function is a function that calls itself. 

In the next article, I am going to discuss the most frequently asked Delegates Interview Questions and Answers in C#. Here, in this article, I try to explain the most frequently asked Functions, Fields, and Constants Interview Questions and Answers in C#. I hope you enjoy these Functions, Fields, and Constants Interview Questions and Answers article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *