C#Interview Questions2
C#Interview Questions2
using System;
namespace Interfaces
{
interface IBankCustomer
{
void DepositMoney();
void WithdrawMoney();
}
public class Demo : IBankCustomer
{
public void DepositMoney()
{
Console.WriteLine("Deposit Money");
}
Notice that method declarations does not have access modifiers like public, private, etc.
By default all interface members are public. It is a compile time error to use access
modifiers on interface member declarations. Also notice that the interface methods have
only declarations and not implementation. It is a compile time error to provide
implementation for any interface member. In our example as the Demo class is inherited
from the IBankCustomer interface, the Demo class has to provide the implementation for
both the methods (WithdrawMoney() and DepositMoney()) that is inherited from the
interface. If the class fails to provide implementation for any of the inherited interface
member, a compile time error will be generated. Interfaces can consist of methods,
properties, events, indexers, or any combination of those four member types. When a
class or a struct inherits an interface, the class or struct must provide implementation for
all of the members declared in the interface. The interface itself provides no functionality
that a class or struct can inherit in the way that base class functionality can be inherited.
However, if a base class implements an interface, the derived class inherits that
implementation.
using System;
namespace Interfaces
{
interface Interface1
{
void Interface1Method();
}
interface Interface2
{
void Interface2Method();
}
class BaseClass1
{
public void BaseClass1Method()
{
Console.WriteLine("BaseClass1 Method");
}
}
class BaseClass2
{
public void BaseClass2Method()
{
Console.WriteLine("BaseClass2 Method");
}
}
//Error : A class cannot inherit from more than one class at the same time
//class DerivedClass : BaseClass1, BaseClass2
//{
//}
//A class can inherit from more than one interface at the same time
public class Demo : Interface1, Interface2
{
public void Interface1Method()
{
Console.WriteLine("Interface1 Method");
}
2. When a class or struct inherits an interface, it inherits only the method names and
signatures, because the interface itself contains no implementations.
If a class inherits an interface, what are the 2 options available for that class?
Option 1: Provide Implementation for all the members inheirted from the interface.
namespace Interfaces
{
interface Interface1
{
void Interface1Method();
}
Option 2: If the class does not wish to provide Implementation for all the members
inheirted from the interface, then the class has to be marked as abstract.
namespace Interfaces
{
interface Interface1
{
void Interface1Method();
}
A class inherits from 2 interfaces and both the interfaces have the same method
name as shown below. How should the class implement the drive method for both
Car and Bus interface?
namespace Interfaces
{
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}
To implement the Drive() method use the fully qualified name as shown in the example
below. To call the respective interface drive method type cast the demo object to the
respective interface and then call the drive method.
using System;
namespace Interfaces
{
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}
using System;
namespace Interfaces
{
interface Car
{
void Drive();
}
//DemoObject.Drive();
// Error: Cannot call explicitly implemented interface method
// using the class object.
// Type cast the demo object to interface type Car
((Car)DemoObject).Drive();
}
}
}
C# Interview Questions on partial classes, structs and methods.
What is a partial class. Give an example?
A partial class is a class whose definition is present in 2 or more files. Each source file
contains a section of the class, and all parts are combined when the application is
compiled. To split a class definition, use the partial keyword as shown in the example
below. Student class is split into 2 parts. The first part defines the study() method and the
second part defines the Play() method. When we compile this program both the parts will
be combined and compiled. Note that both the parts uses partial keyword and public
access modifier.
using System;
namespace PartialClass
{
public partial class Student
{
public void Study()
{
Console.WriteLine("I am studying");
}
}
public partial class Student
{
public void Play()
{
Console.WriteLine("I am Playing");
}
}
public class Demo
{
public static void Main()
{
Student StudentObject = new Student();
StudentObject.Study();
StudentObject.Play();
} }}
It is very important to keep the following points in mind when creating partial
classes.
1. All the parts must use the partial keyword.
2. All the parts must be available at compile time to form the final class.
3. All the parts must have the same access modifiers - public, private, protected etc.
4. Any class members declared in a partial definition are available to all the other parts.
5. The final class is the combination of all the parts at compile time.
What are the advantages of using partial classes?
1. When working on large projects, spreading a class over separate files enables multiple
programmers to work on it at the same time.
2. When working with automatically generated source, code can be added to the class
without having to recreate the source file. Visual Studio uses this approach when it
creates Windows Forms, Web service wrapper code, and so on. You can create code that
uses these classes without having to modify the file created by Visual Studio.
No, a compile time error will be generated stating "Cannot create an instance of the
abstract class or interface "PartialClass.Student". This is because, if any part is declared
abstract, then the whole class becomes abstract. Similarly if any part is declared sealed,
then the whole class becomes sealed and if any part declares a base class, then the whole
class inherits that base class.
class ContainerClass
{
public partial class Nested
{
void Test1() { }
}
public partial class Nested
{
void Test2() { }
}
}
The following are the points to keep in mind when creating partial methods.
1. Partial method declarations must begin partial keyword.
2. The return type of a partial method must be void.
3. Partial methods can have ref but not out parameters.
4. Partial methods are implicitly private, and therefore they cannot be virtual.
5. Partial methods cannot be extern, because the presence of the body determines whether
they are defining or implementing.
using System;
namespace Nested
{
class ContainerClass
{
class InnerClass
{
public string str = "A string variable in nested class";
}
class Demo
{
public static void Main()
{
InnerClass nestedClassObj = new InnerClass();
Console.WriteLine(nestedClassObj.str);
}
}
}
No, the above code will generate a compile time error stating - The type or namespace
name 'InnerClass' could not be found (are you missing a using directive or an assembly
reference?). This is bcos InnerClass is inside ContainerClass and does not have any
access modifier. Hence inner class is like a private member inside ContainerClass. For
the above code to compile and run, we should make InnerClass public and use the fully
qualified name when creating the instance of the nested class as shown below.
using System;
namespace Nested
{
class ContainerClass
{
public class InnerClass
{
public string str = "A string variable in nested class";
}
}
class Demo
{
public static void Main()
{
ContainerClass.InnerClass nestedClassObj = new ContainerClass.InnerClass();
Console.WriteLine(nestedClassObj.str);
}
}
}
Can the nested class access, the Containing class. Give an example?
Yes, the nested class, or inner class can access the containing or outer class as shown in
the example below. Nested types can access private and protected members of the
containing type, including any inherited private or protected members.
using System;
namespace Nested
{
class ContainerClass
{
string OuterClassVariable = "I am an outer class variable";
class Demo
{
public static void Main()
{
ContainerClass.InnerClass nestedClassObj = new ContainerClass.InnerClass();
}
}
}
Output:
I am a container class
I am an inner class
I am a Demo class
The above program has used the concepts of inheritance and nested classes. The
ContainerClass is at the top in the inheritance chain. The nested InnerClass derives from
outer ContainerClass. Finally the DemoClass derives from nested InnerClass. As all the 3
classes are related by inheritance we have the above output.
Usually in .NET, the CLR takes care of memory management. Is there any need for a
programmer to explicitly release memory and resources? If yes, why and how?
If the application is using expensive external resource, it is recommend to explicitly release the
resource before the garbage collector runs and frees the object. We can do this by implementing
the Dispose method from the IDisposable interface that performs the necessary cleanup for the
object. This can considerably improve the performance of the application.
When do we generally use destructors to release resources?
If the application uses unmanaged resources such as windows, files, and network connections,
we use destructors to release resources.
Click here for Written Test or practical ASP.NET Interview Questions asked in MNC's
In C#, What will happen if you do not explicitly provide a constructor for a class?
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.
We cannot create instances of static classes. Can we have constructors for static
classes?
Yes, static classes can also have constructors.
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()
{
}
}
}
using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass(string str)
{
Console.WriteLine(str);
}
}
If a child class instance is created, which class constructor is called first - base class
or child class?
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();
}
}
}
using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass(string str)
{
Console.WriteLine(str);
}
}
class ChildClass : BaseClass
{
//Call the base class contructor from child class
public ChildClass() : base("A call to base class constructor")
{
Console.WriteLine("I am a child class constructor");
}
public static void Main()
{
ChildClass CC = new ChildClass();
}
}
}
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();
}
}
}
Click here for Written Test or practical ASP.NET Interview Questions asked in MNC's
}
public void 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.
What is the difference between method parameters and method arguments. Give an
example?
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;
}
}
}
Explain the difference between passing parameters by value and passing parameters
by reference with an example?
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);
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;
}
}
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;
}
}
}
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.
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");
}
}
}
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
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;
}
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);
}
}
using System;
class Customer
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
using System;
abstract class Customer
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
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 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;
}
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
No, PI is readonly. You can only read the value of PI in the Main() method. You
cannot assign any value to PI.
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
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.
Can derived classes have greater accessibility than their base types?
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, you cannot specify access modifer for an interface member. Interface
members are always public.
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
Is C# a strongly-typed language?
Yes
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
If you define a user defined data type by using the struct keyword, Is it a a
value type or reference type?
Value Type
If you define a user defined data type by using the class keyword, Is it a a
value type or reference type?
Reference type
What is the base class from which all value types are derived?
System.ValueType
What are the differences between value types and reference types?
1. Value types are stored on the stack where as reference types are stored on
the managed heap.
2. Value type variables directly contain their values where as reference variables
holds only a reference to the location of the object that is created on the
managed heap.
3. There is no heap allocation or garbage collection overhead for value-type
variables. As reference types are stored on the managed heap, they have the
over head of object allocation and garbage collection.
4. Value Types cannot inherit from another class or struct. Value types can only
inherit from interfaces. Reference types can inherit from another class or
interface.
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
What type of data type conversion happens when the compiler encounters
the following code?
ChildClass CC = new ChildClass();
ParentClass PC = new ParentClass();
double d = 9999.11;
int i = (int)d; //Cast double to int.
// Explicit conversion is required to cast back to derived type. The code below will
compile but throw an exception at run time if the right-side object is not a Car
object.
Car C2 = (Car) V;
What operators can be used to cast from one reference type to another
without the risk of throwing an exception?
The is and as operators can be used to cast from one reference type to another
without the risk of throwing an exception.
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
What is an array?
An array is a data structure that contains several variables of the same type.
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
System.Console.WriteLine(s1);
// Output: First String Second String
The output of the above code is "Hello" and not "Hello C#". This is bcos, if you
create a reference to a string, and then "modify" the original string, the reference
will continue to point to the original object instead of the new object that was
created when the string was modified.
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
using System;
public class MainClass
{
public static void Main()
{
int Number = 10;
Console.WriteLine(Number.ToString());
}
}
In the above example Number.ToString() method will correctly give the string
representaion of int 10, when you call the ToString() method.
If you have a Customer class as shown in the below example and when you call
the ToString() method the output doesnot make any sense. Hence you have to
override the ToString() method, that is inherited from the System.Object class.
using System;
public class Customer
{
public string FirstName;
public string LastName;
}
public class MainClass
{
public static void Main()
{
Customer C = new Customer();
C.FirstName = "David";
C.LastName = "Boon";
Console.WriteLine(C.ToString());
}
}
The code sample below shows how to override the ToString() method in a class,
that would give the output you want.
using System;
public class Customer
{
public string FirstName;
public string LastName;
Conclusion : If you have a class or a struct, make sure you override the
inherited ToString() method.
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
Can you access a hidden base class method in the derived class?
Yes, Hidden base class methods can be accessed from the derived class by
casting the instance of the derived class to an instance of the base class as
shown in the example below.
using System;
public class BaseClass
{
public virtual void Method()
{
Console.WriteLine("I am a base class method.");
}
}
public class DerivedClass : BaseClass
{
public new void Method()
{
Console.WriteLine("I am a child class method.");
}
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
No, if a class has even a single abstract member, the class has to be marked
abstract. Hence the above code will generate a compile time error stating
"Customer.Test() is abstract but it is contained in nonabstract class Customer"
When an abstract class inherits a virtual method from a base class, the abstract
class can override the virtual method with an abstract method. If a virtual method
is declared abstract, it is still virtual to any class inheriting from the abstract class.
A class inheriting an abstract method cannot access the original implementation
of the method. In the above example, Method() on class NonAbstractChildClass
cannot call Method() on class BaseClass. In this way, an abstract class can force
derived classes to provide new method implementations for virtual methods.
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
What is the base type from which all structs inherit directly?
All structs inherit directly from System.ValueType, which inherits from
System.Object.
Click here for Written Test or practical ASP.NET Interview Questions asked
in MNC's
If you donot specify an access modifier for a method, what is the default
access modifier?
private