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

What is the difference between WindowsDefaultLocation and WindowsDefaultBounds?

WindowsDefaultLocation makes the form to start up at a location selected by the operating system, but with internally specified size. WindowsDefaultBounds delegates both size and starting position choices to the operating system.
Difference between String and string in c#?

String:
1.String is an class(System.String)
2.String is an Reference type(class)

string:
1.string is an alias name of String class that is created by microsoft
2.string is an value type(data type)
3.string is a C# keyword
4.string is a compiler shortcut for System.String class

As per above points when we use string keyword, it reaches the System.String class and then process accordingly, So we can say that both String and string are same.
Difference between String and String Buffer in c#?

String..
1.Its a class used to handle strings.
2.Here concatenation is used to combine two strings.
3.String object is used to concatenate two strings.
4.The first string is combined to the other string by
creating a new copy in the memory as a string object, and
then the old
string is deleted
5.we say "Strings are immutable".
6.When using concatenation consumes more memory.

String Builder..
1.This is also the class used to handle strings.
2.Here Append method is used.
3.Here, Stringbuilder object is used.
4.Insertion is done on the existing string(no new memory is allocated)
5.Usage of StringBuilder is more efficient in case large
amounts of string manipulations have to be performed
6.Consumes less memory when compared to String, if we add the characters to the string in future.
Differences between Show and ShowDialog methods?

These methods are used in Window applications to call one form from another

example: we have Form1 and we want to call Form2.

we may write like this in Form1

private void button1_Click(object s, EventArgs e)
{
Form2 f=new Form2();
f.Show();

}

we can also write f.ShowDialog();

The difference:

Show method does not make the target form (Form2 in this case) as a modal dialog

box. ShowDialog() will make Form2() as a modal dialog box. So, when we use

ShowDialog() method, we cannot click anywhere on Form1 unless we close the

instance of Form2. In case of Show, we can click on Form1 even when Form2 is open.

Can we have statid indexers in C#?

No, indexers are never static in C#.

Indexers always need an object reference to assign or retreive data from arrays
or collections.

example:

class abc
{
int[] arr=new int[2];

//indexer is defined like this
int this[int a]
{
get
{
return arr[a];
}
set
{
arr[a]=value;
}
}
static void Main()
{
abc p=new abc();
//call to indexer's set block.
p[0]=100;

//get block
Console.WriteLine(p[0]);
}

}








}
What is Nullable types in c#?

In C# as we know String is reference type and int is value type. A reference type can be assigned with a null value like string s = null; But you can not assign a null value directly to a integer. like int a = null So to make the value type accept a null value, nullable types are used. To make it nullable, a ? is added . in ? a = null;

properties
Value that gets or sets the value. A DateTime? will have a Value of type DateTime, an int? will have one of type int, etc.
HasValue that returns true if it's not null.

You can always convert a value type to a nullable type:

Ex: DateTime myDate = DateTime.Now;
DateTime? myNullableDate = myDate;
Can we pass default parameter values in C#?

Yes, in C# 4.0, we are provided with the facility to pass parameters with default
values
Consider this example:
using System; 

class Program
{
public static void mymethod(string ename="dd")
{
Console.WriteLine(ename);
}
static void Main()
{
mymethod();
}
}
//Output will be dd;
How can we prevent the conflicts in methods with same names, signatures belonging to multiple interfaces when these interfaces are implemented?

Consider the following code snippet:

Solution:
1)Put the interface name before the method name in the implementation class.
2)remove the modifier public before the method name in the implementation class.

using System;

namespace zz
{
interface i1
{
int add(int a);
}
interface i2
{
int add(int b);
}
class abc:i1,i2
{
int i1.add(int a)
{
return a*5;
}
int i2.add(int b)
{
return b*10;
}
static void Main()
{
i1 p=new abc();
Console.WriteLine(p.add(5));
i2 q=new abc();
Console.WriteLine(q.add(5));
}
}
}


//output:
25
50
For which type of arrays, we can use the params keyword?

To clearly understand the params keyword , consider these examples:

//Correct
1)Single dimensional
static void demod(params int[] k)
{

}

2) Jagged array;

Note that we can use a multidimensional array using a jagged array


//Correct
1) static void demo(params int[][,] k)
{

}
Single dimensional array also OK with a jagged array.
//Correct
2)static void demog(params int[][] k)
{

}
3)Multidimensional array cannot be used directly

//Wrong
static void demok(params int[,] k)
{

}
What is the use of yield keyword in C#?

Yield keyword is used in an iterator block to calculate the value for the enumerator object.

An iterator is a method,
get accessor, or operator that allows us to use foreach loop on object references of user-defined classes.

yield keyword can be used as:

1)yield return <expression>;
or
2)yield break;


code snippet

using System;

//This namespace provides the IEnumerator interface
using System.Collections;
public class demo
{
//GetEnumerator method of IEnumerable interface creates the iterator
public IEnumerator GetEnumerator()
{
for (int i = 1; i <=5; i++)
{
yield return i;
}
}


static void Main()
{

demo t=new demo();

//foreach loop calls the GetEnumerator method
foreach (int i in t)
{
Console.WriteLine(i);
}
}
}


output:

1
2
3
4
5
Does the finally block execute even when we have unhandled exceptions?

Yes, the finally block executes even if the exception is unhandled

Consider this code snippet

using System;


class abc
{
static void Main()
{
try
{
Console.WriteLine("enter");
int a=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("hello"+"---"+a);
}
finally
{
Console.WriteLine("good");
}
}
}


Compile it and run from the Visual sTUDIO Command prompt

put some error value (Example: abc)

The output will consist of the error message and the good string in the
finally block will be displayed.
Can you allow a class to be inherited, but prevent the method from being overridden?

Yes, put 2 modifers before the method sealed override

ex:
class A
{
public virtual void demo()
{
}
}

class B:A
{
public sealed override void demo()
{
}
}
//Class B can be inherited in C, but demo() method cannot be overriden further
class C:B
{


}
Why the Main() method is static?

. The Main() method serves as the entry point of the code.It is automatically loaded and initialised by the CLR when the class loader in the CLR loads the class.If it were not static, an extra object of the class has to be created, that would increase the memory overhead.

. The other reason for being static is to prevent its overriding and not allowing it to be accessed through an object reference of derived class.

. Static will also have compile time memory allocation.
Can DateTime be assigned null ?

Yes we can assign null to a variable of DateTime datatype

For ex :
.Consider this snippet

class A

{
static void Main()
{
DateTime? a=null;
Console.WriteLine("value"+"---"+a);
}
}


//Compile it and execute:

//Explanation:
//DateTime is a nullable data type(it is a value type) whose variable can be assigned null values when declared with ? sign.
In the output, we shall see the string value concatenated with blank string

Can we overload static constructors?

No, static constructors are parameterless constructors and so they cannot
be overloaded.

Static constructors are called before any other class members are called
and called once only.Their main purpose is to initialize only the static members
of a class.
What is the name used to describe the sending of groups of events captured on the client to the server?

NOTE: This is objective type question, Please click question title for correct answer.
Which of the following are forms of state information? a. Application state b. Server state c. Session state

NOTE: This is objective type question, Please click question title for correct answer.
Can we use goto statement in the finally block?

Yes, we can use the goto statement in the finally block.

Code snippet:

class abc

{
static void Main()
{
try
{
Console.WriteLine("welcome");

}

finally
{
goto dd;

dd:
Console.WriteLine("last");
}
}
}


//This block of code will execute since the label dd is inside the finally block.
//if the label dd was outside the finally block, the compiler would have generate
//the error--cannot leave the body of finally clause
using System; class aa { } class bb : aa { } class cc : bb { } Can you create an instance of class aa from the instance of class cc?

Yes, we can create an instance of class aa from
the instance of class cc

add this code snippet
(after the end of the definition of class cc)
class result

{
static void Main(string[] args)
{
cc obj = new cc();
//get the base type of cc

Type b = obj.GetType().BaseType;
//It will be bb
Console.WriteLine(b.Name);

// again get parent for bb type, which is aatype
Type a = b.BaseType;
// create a instance usng Activator.CreateInstance
object o = Activator.CreateInstance(a);


Console.WriteLine(o.GetType().Name);
//It will be aa


}
}

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