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

Can we call Any Function or Method inside Finally Block?

Yes,we can.It will compile successfully.

For example:-

private int sum(int a, int b)

{
try
{
return a + b;
}
catch (Exception ex)
{
throw ex;
}
finally
{
check_value();
}
}

private void check_value()
{
}

What will happen,if we compile below code in C#? private int sum(int a, int b) { try { } catch (Exception ex) { throw ex; } }

NOTE: This is objective type question, Please click question title for correct answer.
What will be the error,if we Compile below function in C# application? private void sum(int a, int b) { try { return a + b; } catch (Exception ex) { throw ex; } }

It will throw an error at Compile-time saying that:-

"Since '_Default.sum(int, int)' returns void, a return keyword must not be followed by an object expression".

So meaning that,if we are creating any function,that returns void means void does not return anything.So we must not return any value from inside function or method.

So we have to remove "return a + b;" from above function.
What will happen,when we compile below C# function? private void check(int id) { try { } catch (Exception ex) { throw ex; } }

NOTE: This is objective type question, Please click question title for correct answer.
Can we write Break or Continue statement outside of For loop?

No, we can not write Break or Continue statement outside of For loop.
It will give a compile-time error as

"No enclosing loop out of which to break or continue."

Because they are the part of For Loop.

So we must write Break or Continue statement inside of For loop.

For Ex:-

public void check_value(int index)

{
for (int i = 0; i < index; i++)
{
int value = 0;
if (i == 2)
{
value = i;
}
}
break;
}


Above function will not compile.
It will give error as

"No enclosing loop out of which to break or continue."

Break statement must be inside for loop. It is used for break current statement will exit the for loop.

For working with above function and successfully compile,make changes as place break inside for loop on the function as below:-

public void check_value(int index)

{
for (int i = 0; i < index; i++)
{
int value = 0;
if (i == 2)
{
value = i;
}
break;
}
}

How to remove only leading space from string?

We can use TrimStart() Dot Net method to remove leading space from string.

For Example:-

string str = " Vishal ";
str = str.TrimStart();
MessageBox.show(str);

Output will be:-

"Vishal "
How to remove only trailing space from string?

We can use TrimEnd() Dot Net method to remove trailing space from string.

For Example:-

string str = " Vishal ";
str = str.TrimEnd();
MessageBox.show(str);

Output will be:-

" Vishal"
Where is the best place to dispose all objects in Code?

Finally block is the best place to release all the objects as

Try

{
}
Catch
{
}
Finally
{
//Always check whether object is null or not.
if (con != null)
con.Close();

if(ds != null)
ds.Dispose();


//Here con is our SqlConnection object and ds is our DataSet object.
}

Which Namespace is used for Sending Mail in Dot Net?

We have to import System.Net.Mail Namespace,which is used for sending mail in Dot Net.

It has MailMessage class,which has To,From,Subject,Body and so on as property.
What will be the output of below code? int total = ((8 - 4) + (4 + 2)) * 2;

NOTE: This is objective type question, Please click question title for correct answer.
What will be the output of below code? int total = 8 - 4 + 4 + 2 * 2;

NOTE: This is objective type question, Please click question title for correct answer.
How to get Client'IP address?

string client_IP_address = Dns.GetHostAddresses(System.Net.Dns.GetHostName()).GetValue(1).ToString();

We have to import System.Net Namespace.
GetHostAddresses returns IP address for the specified Host.
DNS stands for Domain Name Server, which provide Domain Name.
How to Insert any Item inside List at specified position?

With the help of Insert static function of List Collection,we can insert any item.

List<int> values = new List<int>();

values.Add("1"); //Output Would be:: 1
values.Add("3"); //Output Would be:: 1,3

values.Insert(1, "2"); //Output Would be:: 1,2,3

What is the output of below C# code? string str = null; str = str.ToString(); MessageBox.Show(str);

NOTE: This is objective type question, Please click question title for correct answer.
What is the output of below C# code? string str = null; str = Convert.ToString(str); MessageBox.Show(str);

NOTE: This is objective type question, Please click question title for correct answer.
How to remove all the items from a List collection?

With the help of Clear() Static method of List collection,we can remove all the items from List collection.

For Example:-

List<int> lst_id = new List<int>();


lst_id.Add(1);
lst_id.Add(2);

//To remove all items from lst_id
lst_id.Clear();

How to check whether string has a null values or it has not been assigned any values?

We can check,whether string has null value or it does not contain any values with the help of IsNullOrEmpty static method of String Class.

IsNullOrEmpty returns true if string has null value or does not contain any values.

For Example:-

string str;

if(string.IsNullOrEmpty(str))
{
//string is null or empty
}
else
{
//string is not null or it some some values
}

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