C# Interview Questions and Answers (958) - Page 11

What are Generics?

Generics means parametrized types. A class, structure, interface, delegates that operates on a parametrized type is called generics.

The ability to create type-safe code in which type-mismatch errors are caught at compile time rather than run time is the key advantage of the Generics. So using generics on the class, interface, methods or delegates avoids run time errors as it is caught at the compile time itself.

In order to use Generics, we need to work with System.Collections.Generic namespace.

IList<>, IEnumerable, IEnuerator<>, IComparer<>, ICollection<> and IDictionary are some of the frequently used generics objects.
Write the syntax of foreach loop with hashtable and get the key, value pair from the hashtable?

HashTable ht = new HashTable();
ht.Add(1,"A");
ht.Add(2,"B");
ht.Add(3,"C");

foreach(DictionaryEntry de in ht)
{
Console.WriteLine(string.Format("Key : {0} Value: {1}", de.Key.ToString(),de.Value.ToString()));

}
When inheriting from a base class, whether the derived class inherits the destructor and constructor from the base class?

No, On inheriting from a base class the derived class inherits the only the members - including the code except the destructor and constructor of the base class.
What are the Integer Type Supported by c#?

1.sbyte -8 bit signed integer
2.short -16 bit signed integer
3.int -32 bit signed integer
4.long -64 bit signed integer
5.byte -8 bit unsigned integer
6.ushort -16 bit unsigned integer
7.uint -32 bit unsigned integer
8.ulong -64 bit unsigned integer
Explain the states of a window service application?

a. Running: Normal operation occurs during this stage.
b. Paused: The service cannot perform anything beyond till the paused state is changed.
c. Stopped: In this, the service will not work until the service is started once again.
d. Pending: Running is the stage which comes after Pending. Its like waiting for something to run.
Explain what are the types of window services?

There are two types of windows services. Those are Win32OwnProcess , Win32ShareProcess .

a. Win32OwnProcess: It is a Win32 process that can be started by the Service Controller. This type of Win32 service runs in a process by itself.+

b. Win32ShareProcess: It is a Win32 service that allows sharing of processes with other Win32 services.
What is MultiCast Delegate ?

A single or uni cast delegate can be instantiated by only a single method . when a Delegatee can make use of more than one method then it is said to be Multicast Delegate.
What for using System.Logging namespace is used ?

It is used to track the info about , when the particualt application got errorsome. It helps to redirect to the developer inorder to correct those.
When to use Public and Private access modifiers

Using proper access modifiers is the key of writing good Object Oriented Programming.

Below are few of the points that should be taken care while using Public and Private access modifiers

Private
1. A member that are used within a class should be private.
2. If you are refactoring a lengthy method in a class, the re-factored method should be declared as private as you are not intending to reuse that method again.
3. Any member that has some sensitive information or calculations should be controlled by using private and it should be exposed properly through a public member.

Public
1. Frequently used methods should be public.
2. The methods of different layers that are intended to pass data between layers should be declared as public.
3. Generally properties in C# is declared as public as it is intended to use by anyone.
4. Method that gets and sets the value of private data should be public. (For example, Properties)
Define different Access modifiers in C#

The access modifier is the key of Object Oriented Programming, to promote encapsulation, a type in C# should limit its accessibility and this accessibility limit are specified using Access modifiers.

They are - Public, Internal, Protected, Private, Protected Internal

Public:
When used that type (method or variable) can be used by any code throughout the project without any limitation. This is the widest access given to any type and we should use it very judiciously. By default, enumerations and interface has this accessibility.

Internal
When used that type can be used only within the assembly or a friend assembly (provided accessibility is specified using InternalVisibleTo attribute). Here assembly means a .dll.
For example if you have a .dll for a Data Access Layer project and you have declared a method as internal, you will not be able to use that method in the business access layer)
Similarly, if you have a website (not web application) and you have declared internal method in a class file under App_Code then you will not be able to use that method to another class as all classes in website is compiled as a separate .dll when first time it is called).

Internal is more limited than Public.

Protected
When used that type will be visible only within that type or sub type. For example, if you have a aspx page and you want to declare a variable in the code behind and use it in aspx paeg, you will need to declare that variable as at least protected (private will not work) as code behind file is inherited (subclass) by the aspx page.

Protected is more secure than Internal.

Private
When used that type will be visible only within containing type. Default access modifier for methods, variables. For example, if you have declared a variable inside a method, that will be a private variable and may not be accessible outside that method.

Private is the most secure modifiers than any other access modifiers as it is not accessible outside.

Protected Internal
This is not a different type of access modifiers but union of protected and internal. When used this type will be accessible within class or subclass and within the assembly.
When does GarbageCollector runs ?

Garbage Collector runs :

1. When the system has low physical memory
2. Acceptable memory heap is increased.
3. GC.Collect method is called.
What is properties? What are the different types of properties?

Properties provides the facility to protect a field in a class by reading and writing to it through a feature called properties.

Read / Write Property .
Read - Only Property .
Write - Only Property.
Write an equivalent of exit() for quitting a C# .NET application?

Yes, in case of windows forms application you can exit the application with the help of System.Environment.Exit(datatype exitCode) or Application.Exit().
Where the global assembly cache located on the system?

Usually the global assembly cache located at C:\winnt\assembly or C:\windows\assembly.
Where the shared assemblies stored?

The shared assemblies are stored in Global assembly cache.
What’s the C# syntax to catch any possible exception?

A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}.
What are the advantages of using Properties in C#.Net?

The advantages of using properties are:

• Before allowing a change in data, the properties can validate the data.
• Properties can evidently make visible of data on a class from where that data is actually retrieved such as a database.
• Properties can also provide events when data is changed, such as raising an event or changing the value of other fields.
What do you mean by static property? Give an example?

A property with a static keyword is considered as static property. This makes the property available to access at any time, even if no instance of the class exists. Below is the example for a static property.

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

class Value
{
private static int i = 3;
public static int i
{
et
{
return i;
}
}
}

class Main
{
public static void Main()
{
Console.WriteLine(Value.i);
}
}

What do you mean by virtual property? Give an example?

A property declared with virtual keyword is considered as virtual property. These enables derived classes to override the property behavior by using the override keyword. 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;

using System.Collections.Generic;
using System.Linq;
using System.Text;

class Employee
{
private string fName = string.Empty;
Private string lName = string.Empty;

public string FirstName
{
get
{
return fName;
}
set
{
fName = value;
}
}

public string LastName
{
get
{
return lName;
}
set
{
lName = value;
}
}
// Here take FullName is as a virtual
public virtual string FullName
{
get
{
return fName + ", " + lName;
}
}
}

class Company : Employee
{
// Overiding the FullName virtual property derived from employee class
Public override string FullName
{
get
{
return "Mr. " + FirstName + " " + LastName;
}
}
}

class Main
{
public static void Main()
{
Company CompanyObj = new Company();
CompanyObj.FirstName = "Satish";
CompanyObj.LastName = "Reddy";
Console.WriteLine("Employee Full Name is : " + CompanyObj.FullName);
}
}

Explain about abstract property. Give an example?

A property with abstract keyword is considered as abstract property. An abstract property cannot have any implementation in the class. In derived classes you must have to write their own implementation.

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

abstract class Employee
{
private string fName = string.Empty;
private string lName = string.Empty;

public string FirstName
{
get
{
return fName;
}
set
{
fName = value;
}
}

public string LastName
{
get
{
return lName;
}
set
{
lName = value;
}
}
}

// FullName is abstract

public abstract string FullName
{
get;
}
}

class Company: Employee
{
// Overiding the FullName abstract property derived from employee class
public override string FullName
{
get
{
return "Mr. " + FirstName + " " + LastName;
}
}
}

class Main
{
public static void Main()
{
Company CompanyObj = new Company();
CompanyObj.FirstName = "Satish";
CompanyObj.LastName = "Reddy";
Console.WriteLine("Employee Full Name is : " + CompanyObj.FullName);
}
}

Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories