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

__________ and __________ introduced with C#4.0 as a new features?

NOTE: This is objective type question, Please click question title for correct answer.
How can we identify partial classes in C#?

The class declaration with the keyword partial is denoting that the class is a partial class. Physically the partial classes can be available in different files/locations. The C# compiler treat them as a single class at the time of compilation.


public partial class PartialClassExample
{
public void MethodOne(int input)
{
///Do something.
}
}

public partial class PartialClassExample
{
public string MethodTwo(string input)
{
return string.Format("Hello, {0}", input);
}
}


The PartialClassExample can be sit in different files but at the time of compilation they will be merged together.

Hope this helps!
How to check whether Date is valid or not?

By using DateTime.Parse Method,we can check whether Date is valid or not.DateTime.Parse method takes Date a parameter and if given date is valid format then it parses otherwise it throws an error.
For Example:-Below function will return true if valid date otherwise return false.
protected bool Check_Valid_Date(String date)

{
try
{
DateTime dt = DateTime.Parse(date);

return true;
}
catch
{
return false;
}
}

What is the use of DISPOSE method?

Dispose method belongs to ‘IDisposable’ interface. We had seen in the previous section how bad
it can be to override the finalize method for writing the cleaning of unmanaged resources. So if
any object wants to release its unmanaged code best is to implement I Disposable and override
the Dispose method of I Disposable interface. Now once your class has exposed the Dispose
method it is the responsibility of the client to call the Dispose method to do the cleanup.
In c# multiple inheritance is achieved by which of the following?

NOTE: This is objective type question, Please click question title for correct answer.
What is the purpose of ISVirtual property in C# Reflection?

The IsVirtual property of MethodBase class gets a value indicating whether the method is virtual.It returns true if this method is virtual; otherwise, false. It is defined as
public bool IsVirtual { get; }

e.g.
typeof(Employee)

.GetProperties()
.Where(p => p.GetAccessors()[0].IsVirtual)
.ToList()
.ForEach(i => Console.WriteLine(i.Name));

What is the purpose of IsFinal property in C# Reflection?

The IsFinal property of MethodBase class gets a value indicating whether this method is final.It returns true if this method is final; otherwise, false. It is defined as
public bool IsFinal { get; }

e.g.

Suppose we have

public class Employee : IMyInterface

{
public int property1 { get; set; }
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public virtual int EmployeeAge { private set; get; } //read-only property
public virtual string EmployeeSalary { set; private get; } //write-only property
}

interface IMyInterface
{
int property1 { get; set; }

}

And we want to get only the interface property name. Use the below code
typeof(Employee)

.GetProperties()
.Where(p => p.GetAccessors()[0].IsFinal)
.ToList()
.ForEach(i => Console.WriteLine(i.Name));

How to get only Interface methods

NOTE: This is objective type question, Please click question title for correct answer.
List out the benefits of Private Constructors

NOTE: This is objective type question, Please click question title for correct answer.
Explain Param Array with example

If we want to pass variable number of arguments from calling function to called function, we can use the Params Arrays

e.g.
public class ParamTest

{
static void Main()
{
//calling function
PerformArithmeticOperation(20,10,"+"); //perform addition
PerformArithmeticOperation(20,10,"-"); //perform subtraction
PerformArithmeticOperation(20,10,"*"); //perform multiplication
PerformArithmeticOperation(20,10,"/"); //perform division
}

//called function
public static void PerformArithmeticOperation(params object[] items)
{
var result = 0;
switch (Convert.ToString(items[2])) //items[2] is the operator(s) i.e. +,-,*,/
{
case '+': result = Convert.ToInt32(items[0]) + Convert.ToInt32(items[1]); break;
case '-': result = Convert.ToInt32(items[0]) - Convert.ToInt32(items[1]); break;
case '*': result = Convert.ToInt32(items[0]) * Convert.ToInt32(items[1]); break;
case '/': result = Convert.ToInt32(items[0]) / Convert.ToInt32(items[1]); break;
default: Console.WriteLine("No such operation available");
}
Console.WriteLine(result);
}
}


You also can send no arguments. If you send no arguments, the length of the params list is zero.
What is using statement in C#?

In C# parlance, Using can be use in two ways
a) As a directive where it's behaviour will be as inclusion of a namespace. e.g.

using System; //import namespace

using System.IO;
using Item = System.Collections.Generic; //creating alias for namespace

namespace UsingNamespaceDemo
{
public class TestUsingClass
{
var intItemList = new Item.List<int>(){1,2,3,4,5}; //observe Item.List where Item is an alias of System.Collections.Generic namespace
foreach(var item in intItemList) Console.WriteLine(item);
}
}


b)As a statement that can only be used with types that implement IDisposable interface.
e.g.

string connString = "Data Source=abc;Integrated Security=SSPI;Initial Catalog=db;";

using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM tblTest";
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
Console.WriteLine("{0}", dr.GetString(0));
}
}


It can also be consider as a syntactic sugar(short hand version) for try/finally block e.g.
using (SqlConnection conn = new SqlConnection(connString))

{
//do something
}


is same as
SqlConnection conn = new SqlConnection(connString);

try
{
//do something
}
finally
{
if(conn != null)
{
conn.Dispose();
}
}

What is Yield Statement?

It performs ITERATION .
When we use the yield keyword in a statement, we indicate that the method, operator, or get accessor in which it appears is an iterator.
It creates a state machine "under the covers" that remembers where you were on each additional cycle of the function and picks up from there. In short,Yield has two great uses

a) It helps to provide custom iteration with out creating temp collections.

b) It helps to do stateful iteration.

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