Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

201.

What Are The 2 Broad Classifications Of Data Types Available In C#?

Answer»

1. Built in data TYPES.
2. USER defined data types.

1. Built in data types.
2. User defined data types.

202.

Is C# A Strongly-typed Language?

Answer»

Yes.

Yes.

203.

What Are The 3 Types Of Comments In C#?

Answer»

1. Single Line Comments. You DEFINE single line comments with // as shown below.
//This is an example for single line COMMENT
2. MULTI line comments. You define multi line comments with /* */ as shown below.
/*This is an example for Multi Line comments*/
3. XML Comments. You define XML comments with /// as shown below. ///This is an example for defining XML comments.

1. Single Line Comments. You define single line comments with // as shown below.
//This is an example for single line comment
2. Multi line comments. You define multi line comments with /* */ as shown below.
/*This is an example for Multi Line comments*/
3. XML Comments. You define XML comments with /// as shown below. ///This is an example for defining XML comments.

204.

Can You Specify An Access Modifier For An Enumeration?

Answer»

Enumeration MEMBERS are always public, and no ACCESS modifiers can be SPECIFIED.

Enumeration members are always public, and no access modifiers can be specified.

205.

What Is The Default Access Modifier For A Class,struct And An Interface Declared Directly With A Namespace?

Answer»

internal.

internal.

206.

What Does Protected Internal Access Modifier Mean?

Answer»

The protected internal ACCESS MEANS protected OR internal, not protected AND internal. In simple terms, a protected internal member is accessible from any class in the same assembly, INCLUDING DERIVED classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its MEMBERS as protected.

The protected internal access means protected OR internal, not protected AND internal. In simple terms, a protected internal member is accessible from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected.

207.

Can Destructors Have Access Modifiers?

Answer»

No, DESTRUCTORS cannot have ACCESS MODIFIERS.

No, destructors cannot have access modifiers.

208.

Can The Accessibility Of A Type Member Be Greater Than The Accessibility Of Its Containing Type?

Answer»

No, the accessibility of a type member can never be greater than the accessibility of its containing type. For EXAMPLE, a PUBLIC METHOD declared in an INTERNAL class has only internal accessibility.

No, the accessibility of a type member can never be greater than the accessibility of its containing type. For example, a public method declared in an internal class has only internal accessibility.

209.

Can You Declare Struct Members As Protected?

Answer»

No, STRUCT members cannot be DECLARED protected. This is because structs do not SUPPORT INHERITANCE.

No, struct members cannot be declared protected. This is because structs do not support inheritance.

210.

Can Derived Classes Have Greater Accessibility Than Their Base Types?

Answer»

No, Derived classes cannot have greater accessibility than their base types. For example the FOLLOWING code is illegal. using System;
internal class InternalBaseClass
{
public void Print()
{
Console.WriteLine("I am a Base Class Method");
}
}
public class PublicDerivedClass : InternalBaseClass
{
public static void Main()
{
Console.WriteLine("I am a Public Derived Class Method");
}
}

When you compile the above code an error will be generated stating "INCONSISTENT accessibility: base class InternalBaseClass is less accessible than class PublicDerivedClass".To make this SIMPLE, you cannot have a public class B that DERIVES from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

No, Derived classes cannot have greater accessibility than their base types. For example the following code is illegal. using System;
internal class InternalBaseClass
{
public void Print()
{
Console.WriteLine("I am a Base Class Method");
}
}
public class PublicDerivedClass : InternalBaseClass
{
public static void Main()
{
Console.WriteLine("I am a Public Derived Class Method");
}
}

When you compile the above code an error will be generated stating "Inconsistent accessibility: base class InternalBaseClass is less accessible than class PublicDerivedClass".To make this simple, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

211.

Can You Use All Access Modifiers For All Types?

Answer»

No, Not all access modifiers can be used by all TYPES or MEMBERS in all CONTEXTS, and in some cases the ACCESSIBILITY of a type member is constrained by the accessibility of its containing type.

No, Not all access modifiers can be used by all types or members in all contexts, and in some cases the accessibility of a type member is constrained by the accessibility of its containing type.

212.

What Are Access Modifiers Used For?

Answer»

ACCESS MODIFIERS are USED to control the accessibilty of types and MEMBERS with in the types.

Access Modifiers are used to control the accessibilty of types and members with in the types.

213.

What Are Access Modifiers In C#?

Answer»

In C# there are 5 different types of Access Modifiers.
PUBLIC
The public type or member can be accessed by any other code in the same assembly or another assembly that REFERENCES it.

PRIVATE
The type or member can only be accessed by code in the same class or struct.

PROTECTED
The type or member can only be accessed by code in the same class or struct, or in a derived class.

INTERNAL
The type or member can be accessed by any code in the same assembly, but not from another assembly.

Protected Internal
The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

In C# there are 5 different types of Access Modifiers.
Public
The public type or member can be accessed by any other code in the same assembly or another assembly that references it.

Private
The type or member can only be accessed by code in the same class or struct.

Protected
The type or member can only be accessed by code in the same class or struct, or in a derived class.

Internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.

Protected Internal
The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

214.

What Is The Difference Between A Constant And A Static Readonly Field?

Answer»

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.

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.

215.

What Is Wrong With The Sample Program Below?

Answer»

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.

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.

216.

Can You Declare A Field Readonly?

Answer»

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

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

217.

What Is A Static Field?

Answer»

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.

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.

218.

What Are Instance Fields In C#?

Answer»

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.

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.

219.

What Are The 2 Broad Classifications Of Fields In C#?

Answer»

1. INSTANCE FIELDS
2. STATIC fields

1. Instance fields
2. Static fields

220.

How Do You Access A Constant Field Declared In A Class?

Answer»

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

using System;
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);
}
}

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

using System;
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);
}
}

221.

Can You Change The Value Of A Constant Filed After Its Declaration?

Answer»

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 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 run time, const fields cannot be passed by reference.

USING System;
class Circle
{
public const double PI = 3.14;
}

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 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 run time, const fields cannot be passed by reference.

using System;
class Circle
{
public const double PI = 3.14;
}

222.

Does C# Support Const Methods, Properties, Or Events?

Answer»

No, C# does not support const METHODS, PROPERTIES, or EVENTS.

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

223.

Can You Declare A Class Or A Struct As Constant?

Answer»

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

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

224.

What Are Constants In C#?

Answer»

Constants in C# are immutable values which are known at compile time and do not CHANGE for the life of the program. 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 isdeclared. An example is shown below.

using System;
CLASS Circle
{
PUBLIC const double PI = 3.14;
public Circle()
{
//Error : You can only assign a value to a constant field at the time of declaration
//PI = 3.15;
}
}
class MainClass
{
public static void Main()
{
Console.WriteLine(Circle.PI);
}
}

Constants in C# are immutable values which are known at compile time and do not change for the life of the program. 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 isdeclared. An example is shown below.

using System;
class Circle
{
public const double PI = 3.14;
public Circle()
{
//Error : You can only assign a value to a constant field at the time of declaration
//PI = 3.15;
}
}
class MainClass
{
public static void Main()
{
Console.WriteLine(Circle.PI);
}
}

225.

Can You Use Virtual, Override Or Abstract Keywords On An Accessor Of A Static Property?

Answer»

No, it is a COMPILE time error to use a VIRTUAL, ABSTRACT or override KEYWORDS on an accessor of a static PROPERTY.

No, it is a compile time error to use a virtual, abstract or override keywords on an accessor of a static property.

226.

What Is An Abstract Property. Give An Example?

Answer»

A property that is MARKED with abstract KEYWORD is considered abstract property. An abstract property should not have any IMPLEMENTATION in the class. The derived classes must write their own implementation. In the example below FullName property is abstract in the Customer class. BankCustomer class overrides the INHERITED abstract FullName property with its own implementation.

using System;
abstract class Customer
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
// FullName is abstract
public abstract string FullName
{
get;
}
}
class BankCustomer : Customer
{
// Overiding the FullName abstract property derived from customer class
public override string FullName
{
get
{
return "Mr. " + FirstName + " " + LastName;
}
}
}
class MAINCLASS
{
public static void Main()
{
BankCustomer BankCustomerObject = new BankCustomer();
BankCustomerObject.FirstName = "David";
BankCustomerObject.LastName = "Boon";
Console.WriteLine("Customer Full Name is : " + BankCustomerObject.FullName);
}
}

A property that is marked with abstract keyword is considered abstract property. An abstract property should not have any implementation in the class. The derived classes must write their own implementation. In the example below FullName property is abstract in the Customer class. BankCustomer class overrides the inherited abstract FullName property with its own implementation.

using System;
abstract class Customer
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
// FullName is abstract
public abstract string FullName
{
get;
}
}
class BankCustomer : Customer
{
// Overiding the FullName abstract property derived from customer class
public override string FullName
{
get
{
return "Mr. " + FirstName + " " + LastName;
}
}
}
class MainClass
{
public static void Main()
{
BankCustomer BankCustomerObject = new BankCustomer();
BankCustomerObject.FirstName = "David";
BankCustomerObject.LastName = "Boon";
Console.WriteLine("Customer Full Name is : " + BankCustomerObject.FullName);
}
}

227.

What Is A Virtual Property. Give An Example?

Answer»

A property that is marked with virtual keyword is considered virtual property. Virtual properties enable derived classes to override the property behavior by using the override keyword. In the example below FullName is virtual property in the Customer class. BankCustomer class inherits from Customer class and overrides the FullName virtual property. In the output you can see the over riden IMPLEMENTATION. A property overriding a virtual property can also be sealed, specifying that for derived classes it is no longer virtual.

using System;
class Customer
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
public string FirstName
{
GET
{
return _firstName;
}
set
{
_firstName = value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
// FullName is virtual
public virtual string FullName
{
get
{ return _lastName + ", " + _firstName;
}
}
}
class BankCustomer : Customer
{
// Overiding the FullName virtual property derived from customer class
public override string FullName
{
get
{
return "MR. " + FirstName + " " + LastName;
}
}
}
class MainClass
{
public static void MAIN()
{
BankCustomer BankCustomerObject = new BankCustomer();
BankCustomerObject.FirstName = "David";
BankCustomerObject.LastName = "Boon";
Console.WriteLine("Customer Full Name is : " + BankCustomerObject.FullName);
}
}

A property that is marked with virtual keyword is considered virtual property. Virtual properties enable derived classes to override the property behavior by using the override keyword. In the example below FullName is virtual property in the Customer class. BankCustomer class inherits from Customer class and overrides the FullName virtual property. In the output you can see the over riden implementation. A property overriding a virtual property can also be sealed, specifying that for derived classes it is no longer virtual.

using System;
class Customer
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
// FullName is virtual
public virtual string FullName
{
get
{ return _lastName + ", " + _firstName;
}
}
}
class BankCustomer : Customer
{
// Overiding the FullName virtual property derived from customer class
public override string FullName
{
get
{
return "Mr. " + FirstName + " " + LastName;
}
}
}
class MainClass
{
public static void Main()
{
BankCustomer BankCustomerObject = new BankCustomer();
BankCustomerObject.FirstName = "David";
BankCustomerObject.LastName = "Boon";
Console.WriteLine("Customer Full Name is : " + BankCustomerObject.FullName);
}
}

228.

What Is A Static Property. Give An Example?

Answer»

A PROPERTY that is marked with a static keyword is considered as static property. This makes the property available to CALLERS at any time, even if no instance of the class exists. In the example below PI is a static property.

using System;
class Circle
{
private static double _pi = 3.14;
public static double PI
{
get
{
return _pi;
}
}
}
class MainClass
{
public static VOID Main()
{
Console.WriteLine(Circle.PI);
}
}

A property that is marked with a static keyword is considered as static property. This makes the property available to callers at any time, even if no instance of the class exists. In the example below PI is a static property.

using System;
class Circle
{
private static double _pi = 3.14;
public static double PI
{
get
{
return _pi;
}
}
}
class MainClass
{
public static void Main()
{
Console.WriteLine(Circle.PI);
}
}

229.

What Are The Advantages Of Properties In C#?

Answer»

1. Properties can VALIDATE data before allowing a change.
2. Properties can transparently EXPOSE data on a class where that data is actually retrieved from some other source such as a database.
3. Properties can TAKE an action when data is changed, such as raising an event or changing the VALUE of other FIELDS.

1. Properties can validate data before allowing a change.
2. Properties can transparently expose data on a class where that data is actually retrieved from some other source such as a database.
3. Properties can take an action when data is changed, such as raising an event or changing the value of other fields.

230.

Explain The 3 Types Of Properties In C# With An Example?

Answer»

1. Read Only PROPERTIES: Properties WITHOUT a set accessor are considered read-only. In the above example FullName is read only property.
2. WRITE Only Properties: Properties without a GET accessor are considered write-only. In the above example COUNTRY is write only property.
3. Read Write Properties: Properties with both a get and set accessor are considered read-write properties. In the above example FirstName and LastName are read write properties.

1. Read Only Properties: Properties without a set accessor are considered read-only. In the above example FullName is read only property.
2. Write Only Properties: Properties without a get accessor are considered write-only. In the above example Country is write only property.
3. Read Write Properties: Properties with both a get and set accessor are considered read-write properties. In the above example FirstName and LastName are read write properties.

231.

What Are Properties In C#. Explain With An Example?

Answer»

Properties in C# are class members that provide a FLEXIBLE mechanism to read, write, or COMPUTE the values of private fields. Properties can be used as if they are public data members, but they are actually special methods called ACCESSORS. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

In the example below _firstName and _lastName are private string variables which are accessible only inside the Customer class. _firstName and _lastName are exposed using FirstName and LastName public properties respectively. The get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels. The value keyword is used to define the value being assigned by the set accessor. The FullName property computes the full name of the customer. Full Name property is readonly, because it has only the get accessor. Properties that do not implement a set accessor are read only.

The code block for the get accessor is executed when the property is read and the code block for the set accessor is executed when the property is assigned a new value.

using System;
class Customer
{
// Private fileds not accessible outside the class.
private string _firstName = string.Empty;
private string _lastName = string.Empty;
private string _coutry = string.Empty;
// public FirstName property exposes _firstName variable
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
// public LastName property exposes _lastName variable
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
// FullName property is readonly and computes customer full name.
public string FullName
{
get
{
return _lastName + ", " + _firstName;
}
}
//Country Property is Write Only
public string Country
{
set
{
_coutry = value;
}
}
}
class MainClass
{
public static void Main()
{
Customer CustomerObject = new Customer();
//This line will call the set accessor of FirstName Property
CustomerObject.FirstName = "David";
//This line will call the set accessor of LastName Property
CustomerObject.LastName = "BOON";
//This line will call the get accessor of FullName Property
Console.WriteLine("Customer Full Name is : " + CustomerObject.FullName);
}
}

Properties in C# are class members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

In the example below _firstName and _lastName are private string variables which are accessible only inside the Customer class. _firstName and _lastName are exposed using FirstName and LastName public properties respectively. The get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels. The value keyword is used to define the value being assigned by the set accessor. The FullName property computes the full name of the customer. Full Name property is readonly, because it has only the get accessor. Properties that do not implement a set accessor are read only.

The code block for the get accessor is executed when the property is read and the code block for the set accessor is executed when the property is assigned a new value.

using System;
class Customer
{
// Private fileds not accessible outside the class.
private string _firstName = string.Empty;
private string _lastName = string.Empty;
private string _coutry = string.Empty;
// public FirstName property exposes _firstName variable
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
// public LastName property exposes _lastName variable
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
// FullName property is readonly and computes customer full name.
public string FullName
{
get
{
return _lastName + ", " + _firstName;
}
}
//Country Property is Write Only
public string Country
{
set
{
_coutry = value;
}
}
}
class MainClass
{
public static void Main()
{
Customer CustomerObject = new Customer();
//This line will call the set accessor of FirstName Property
CustomerObject.FirstName = "David";
//This line will call the set accessor of LastName Property
CustomerObject.LastName = "Boon";
//This line will call the get accessor of FullName Property
Console.WriteLine("Customer Full Name is : " + CustomerObject.FullName);
}
}

232.

If A Method's Return Type Is Void, Can You Use A Return Keyword In The Method?

Answer»

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.

using SYSTEM;
namespace DEMO
{
class Program
{
public static void MAIN()
{
SayHi();
}
public static void SayHi()
{
Console.WriteLine("Hi");
return;
Console.WriteLine("This statement will NEVER be executed");
}
}
}

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.

using System;
namespace Demo
{
class Program
{
public static void Main()
{
SayHi();
}
public static void SayHi()
{
Console.WriteLine("Hi");
return;
Console.WriteLine("This statement will never be executed");
}
}
}

233.

Can You Pass Value Types By Reference To A Method?

Answer»

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

USING System;
namespace Demo
{
class Program
{
public STATIC void Main()
{
INT I = 10;
Console.WriteLine("Value of I before passing to the method = " + I);
Function(ref I);
Console.WriteLine("Value of I after passing to the method by reference= " + I);
}
public static void Function(ref int Number)
{
Number = Number + 5;
}
}
}

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

using System;
namespace Demo
{
class Program
{
public static void Main()
{
int I = 10;
Console.WriteLine("Value of I before passing to the method = " + I);
Function(ref I);
Console.WriteLine("Value of I after passing to the method by reference= " + I);
}
public static void Function(ref int Number)
{
Number = Number + 5;
}
}
}

234.

Explain The Difference Between Passing Parameters By Value And Passing Parameters By Reference With An Example?

Answer»

We can pass parameters to a method by value or by reference. By DEFAULT all value types are PASSED by value where as 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.

using System;
NAMESPACE Demo
{
class Program
{
public static void Main()
{
INT I = 10;
int K = Function(I);
Console.WriteLine("I = " + I);
Console.WriteLine("K = " + K);
}
public static int Function(int Number)
{
int ChangedValue = Number + 1;
return ChangedValue;
}
}
}

By default, reference types are passed by reference. When an object of a reference type is passed to a method, the reference 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.

using System;
namespace Demo
{
class Program
{
public static void Main()
{
ReferenceTypeExample Object = new ReferenceTypeExample();
Object.Number = 20;
Console.WriteLine("Original Object Value = " + Object.Number);
Function(Object);
Console.WriteLine("Object Value after passed to the method= " + Object.Number);
}
public static void Function(ReferenceTypeExample ReferenceTypeObject)
{
ReferenceTypeObject.Number = ReferenceTypeObject.Number + 5;
}
}
class ReferenceTypeExample
{
public int Number;
}
}

We can pass parameters to a method by value or by reference. By default all value types are passed by value where as 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.

using System;
namespace Demo
{
class Program
{
public static void Main()
{
int I = 10;
int K = Function(I);
Console.WriteLine("I = " + I);
Console.WriteLine("K = " + K);
}
public static int Function(int Number)
{
int ChangedValue = Number + 1;
return ChangedValue;
}
}
}

By default, reference types are passed by reference. When an object of a reference type is passed to a method, the reference 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.

using System;
namespace Demo
{
class Program
{
public static void Main()
{
ReferenceTypeExample Object = new ReferenceTypeExample();
Object.Number = 20;
Console.WriteLine("Original Object Value = " + Object.Number);
Function(Object);
Console.WriteLine("Object Value after passed to the method= " + Object.Number);
}
public static void Function(ReferenceTypeExample ReferenceTypeObject)
{
ReferenceTypeObject.Number = ReferenceTypeObject.Number + 5;
}
}
class ReferenceTypeExample
{
public int Number;
}
}

235.

What Is The Difference Between Method Parameters And Method Arguments. Give An Example?

Answer»

In the example below FirstNumber and SecondNumber are method parameters where as FN and LN 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.

using System;
namespace Demo
{
class Program
{
public static void Main()
{
int FN = 10;
int SN = 20;
//FN and LN are method arguments
int TOTAL = Sum(FN, SN);
Console.WriteLine(Total);
}
//FirstNumber and SecondNumber are method parameters
public static int Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
return Result;
}
}
}

In the example below FirstNumber and SecondNumber are method parameters where as FN and LN 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.

using System;
namespace Demo
{
class Program
{
public static void Main()
{
int FN = 10;
int SN = 20;
//FN and LN are method arguments
int Total = Sum(FN, SN);
Console.WriteLine(Total);
}
//FirstNumber and SecondNumber are method parameters
public static int Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
return Result;
}
}
}

236.

Is The Following Code Legal?

Answer»

using System;
namespace Demo
{
class Program
{
public static void Main()
{
}
public void Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
public int Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
}
}

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.

using System;
namespace Demo
{
class Program
{
public static void Main()
{
}
public void Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
public int Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
}
}

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.

237.

Does C# Provide Copy Constructor?

Answer»

No, C# does not PROVIDE COPY CONSTRUCTOR.

No, C# does not provide copy constructor.

238.

Give 2 Scenarios Where Static Constructors Can Be Used?

Answer»

1. A typical USE of static CONSTRUCTORS is when the class is USING a LOG file and the constructor is used to write entries to this file.
2. Static constructors are also useful when creating wrapper classes for UNMANAGED code, when the constructor can call the LoadLibrary method.

1. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
2. Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

239.

What Happens If A Static Constructor Throws An Exception?

Answer»

If a STATIC constructor throws an exception, the runtime will not INVOKE it a second time, and the type will remain uninitialized for the LIFETIME of the application DOMAIN in which your program is running.

If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

240.

Can You Have Parameters For Static Constructors?

Answer»

No, STATIC CONSTRUCTORS cannot have PARAMETERS.

No, static constructors cannot have parameters.

241.

Can You Mark Static Constructor With Access Modifiers?

Answer»

No, we cannot USE ACCESS MODIFIERS on STATIC CONSTRUCTOR.

No, we cannot use access modifiers on static constructor.

242.

Can A Class Have Static Constructor?

Answer»

Yes, a class can have static constructor. Static constructors are called automatically, immediately before any static fields are ACCESSED, and are generally used to initialize static class members. It is called automatically before the first instance is created or any static members are REFERENCED. Static constructors are called before instance constructors. An example is shown below.

using System;
namespace TestConsole
{
class Program
{
static int I;
static Program()
{
I = 100;
Console.WriteLine("Static Constructor called");
}
public Program()
{
Console.WriteLine("Instance Constructor called");
}
public static VOID Main()
{
Program P = NEW Program();
}
}
}

Yes, a class can have static constructor. Static constructors are called automatically, immediately before any static fields are accessed, and are generally used to initialize static class members. It is called automatically before the first instance is created or any static members are referenced. Static constructors are called before instance constructors. An example is shown below.

using System;
namespace TestConsole
{
class Program
{
static int I;
static Program()
{
I = 100;
Console.WriteLine("Static Constructor called");
}
public Program()
{
Console.WriteLine("Instance Constructor called");
}
public static void Main()
{
Program P = new Program();
}
}
}

243.

If A Child Class Instance Is Created, Which Class Constructor Is Called First - Base Class Or Child Class?

Answer»

When an instance of a child class is created, the base class constructor is called before the child class constructor. An example is SHOWN below.

using System;
NAMESPACE TestConsole
{
class BASECLASS
{
PUBLIC BaseClass()
{
Console.WriteLine("I am a base class constructor");
}
}
class ChildClass : BaseClass
{
public ChildClass()
{
Console.WriteLine("I am a child class constructor");
}
public static void Main()
{
ChildClass CC = new ChildClass();
}
}
}

When an instance of a child class is created, the base class constructor is called before the child class constructor. An example is shown below.

using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass()
{
Console.WriteLine("I am a base class constructor");
}
}
class ChildClass : BaseClass
{
public ChildClass()
{
Console.WriteLine("I am a child class constructor");
}
public static void Main()
{
ChildClass CC = new ChildClass();
}
}
}

244.

Can A Child Class Call The Constructor Of A Base Class?

Answer»

YES, a child class can call the constructor of a BASE class by using the base keyword as shown in the example below.

using SYSTEM;
NAMESPACE TestConsole
{
class BaseClass
{
public BaseClass(STRING str)
{
Console.WriteLine(str);
}
}
class ChildClass : BaseClass
{
public ChildClass(string str): base(str)
{
}
public static void Main()
{
ChildClass CC = new ChildClass("Calling base class constructor from child class");
}
}
}

Yes, a child class can call the constructor of a base class by using the base keyword as shown in the example below.

using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass(string str)
{
Console.WriteLine(str);
}
}
class ChildClass : BaseClass
{
public ChildClass(string str): base(str)
{
}
public static void Main()
{
ChildClass CC = new ChildClass("Calling base class constructor from child class");
}
}
}

245.

Can A Class Or A Struct Have Multiple Constructors?

Answer»

Yes, a CLASS or a struct can have multiple CONSTRUCTORS. Constructors in csharp can be OVERLOADED.

Yes, a class or a struct can have multiple constructors. Constructors in csharp can be overloaded.

246.

Can You Prevent A Class From Being Instantiated?

Answer»

Yes, a class can be prevented from being instantiated by using a private constructor as SHOWN in the example below.

using SYSTEM;
namespace TestConsole
{
class Program
{
public STATIC void MAIN()
{
//Error cannot create instance of a class with private constructor
SampleClass SC = new SampleClass();
}
}
class SampleClass
{
double PI = 3.141;
private SampleClass()
{
}
}
}

Yes, a class can be prevented from being instantiated by using a private constructor as shown in the example below.

using System;
namespace TestConsole
{
class Program
{
public static void Main()
{
//Error cannot create instance of a class with private constructor
SampleClass SC = new SampleClass();
}
}
class SampleClass
{
double PI = 3.141;
private SampleClass()
{
}
}
}

247.

We Cannot Create Instances Of Static Classes. Can We Have Constructors For Static Classes?

Answer»

YES, STATIC CLASSES can ALSO have CONSTRUCTORS.

Yes, static classes can also have constructors.

248.

Structs Are Not Reference Types. Can Structs Have Constructors?

Answer»

YES, EVEN THOUGH Structs are not REFERENCE types, structs can have constructors.

Yes, even though Structs are not reference types, structs can have constructors.

249.

In C#, What Will Happen If You Do Not Explicitly Provide A Constructor For A Class?

Answer»

If you do not provide a constructor EXPLICITLY for your CLASS, C# will create one by default that instantiates the OBJECT and sets all the member variables to their default VALUES.

If you do not provide a constructor explicitly for your class, C# will create one by default that instantiates the object and sets all the member variables to their default values.

250.

What Is A Constructor In C#?

Answer»

Constructor is a class METHOD that is executed when an object of a class is CREATED. Constructor has the same name as the class, and usually used to INITIALIZE the data MEMBERS of the new object.

Constructor is a class method that is executed when an object of a class is created. Constructor has the same name as the class, and usually used to initialize the data members of the new object.