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

Which of these classes allow us to add a printer dynamically?

NOTE: This is objective type question, Please click question title for correct answer.
How many types of comments are there in C#?

3 Types of comments

1) Single line: //

2) MultiLine: /* */


3) Xml: /// ///
Can we specify the access modifiers for the get and set accessors in a property?

Yes, we can specify the access modifiers for the get and set accessors in a property

Example:

internal string myname
{
private set;
get;


}

This is Ok

Note:
1)The access modifier for the accessor must be more restrictive than that of the property.

private is more restrictive than internal.

2)Both get and set accessors cannot have access modifiers in the same property.

private set and private get would be WRONG in the above example.
Difference between | and ||

| : logical OR

||: Conditional OR

Both operators are used to check the OR condition.

The differences:

1)if the first condition evaluates to TRUE, then

| : it will check the second condition also.

||: it does not check the second condition

2) If the first condition evaluates to FALSE, | and || both will check the second condition

Take these 2 examples:

class dd
{
static void Main()
{

int a = 10, b = 0;

if (a==10 | (a%b)==0)
{
Console.WriteLine("yes");
}
//output will be an error message
}

class dd
{
static void Main()
{

int a = 10, b = 0;

if (a==10 || (a%b)==0)
{
Console.WriteLine("yes");
}
//output will be yes
}
What is the difference between == and Equals?

Both are used for comparison of variables, can be overloaded and return a Boolean value.

The differences are:-

Consider two variables of different types:
1) int a = 20;
string b = "20";

//Compiler error (== cannot be applied to int and string)
Console.WriteLine(a == b);

//output: False
Console.WriteLine(a.Equals(b));

2)int c = 455;
long f = 455;
//True
Console.WriteLine(c == f);
//False
Console.WriteLine(c.Equals(f));

//Equals strictly checks that 2 variables should be of the same type

// == gives True if 2 variables ' data types allow for similar values(Example: int and long)

3)List<string> y = new List<string>();
y.Add("one");
List<string> z = new List<string>();
z.Add("one");
Console.WriteLine(y == z);
Console.WriteLine(y.Equals(z));

Both will give False.
Difference between Type.GetType() and Object.GetType()

Both methods are used for extracting datatype related information and are defined in Type

and Object classes.

Type.GetType() does not need a variable or object reference .It can be directly used.

example:

DataTable dt = new DataTable();
dt.Columns.Add("eno", Type.GetType("System.Int32"));
dt.Columns.Add("ename", Type.GetType("System.String"));

Object.GetType(): needs a variable or an object reference,
example:
int a=30;
//int is indirectly derived from the Object class.

Console.WriteLine(a.GetType().Name);
What is the preferred model for event handling in C#?

NOTE: This is objective type question, Please click question title for correct answer.
Which of these variables are compiled as an anonymous type?

NOTE: This is objective type question, Please click question title for correct answer.
What is Asynchronous call and how it can be implemented using delegates?

The Asynchronous calls wait for a method before the program flow is resumed to complete its task. In an asynchronous call, the program flow continues while the method is executes.

//create an object for a function

Function Fun = new Function();


//create delegate

Delegate Del = new Delegate(Fun.Function);


//invoke the method asynchronously

IAsyncResult call = Delegate.Invoke();

What is ' ?? ' operator ?

It is also called as the null coalescing operator.
It evaluates which value will be assigned to a variable of a particular type if we have 2 variables
of a particular type(nullable or a reference type).

The 1st input variable must be a nullable or a reference type.
The 2nd input variable should be of the same type as the first one or of a type that can be implicitly
converted to the type of the first variable.


input 1 input 2 target
null not null value of 2nd input
not null null value of 1st input
not null not null value of 1st input
null null null
example:
class Class2
{
static void Main()
{
int? a = null;
int b;
int c=30;

b=a ?? c;
Console.WriteLine(b);
//b is 30 as a is null
a = 35;
b = a ?? c;
Console.WriteLine(b);
//b is 35 as a is now assigned 35 value
}

}
How can we underline a particular character in the Text of the Window Label

example: if we want to write Hello on a Label and underline the 'H' character,

1) Type &Hello in the text property

2) Set UseMnemonic property of the Label to true(It is true by default)
How can we make our C# classes to interoperate with other .net languages ?

We must make sure that our C# classes conforms to th Common Language Subset(CLS).

For this we need to add a global assembly to our C# source files

Which can be depicted as :

[assembly: CLSCompliant (true)]


If we use a C# feature which is not CLS-Compilant the compiler will emit an error.
Can C# support variable argument on method ?

Yes it can. Suppose let us take an example of

void paramsMyDemo(object arg1, object arg2, params object[] argsResult)

{
foreach (object arg in argsResult)
{
/* Some Logic */
}
}


Here params keyword is applied on a method parameter which is an array . So now if we want to invoke that method we need to pass the parameters in comma seperated list.

type.paramsMyDemo(2,0.5f, "DotNetFunda", 0.0m, new UserDefinedType());

How is anchoring different from docking?

Docking refers to attaching a control to either an edge(top, right, bottom, or left) or the client area of the parent control. On the other hand, in anchoring you specify the distance that each edge of your control maintains from the edge of the parent control.
How can we disable the context menu for a textbox control?

The Textbox class contains the ContextMenuStrip property. When we set this property to a dummy instance of the ContextMenu class, the textbox control is unable to provide any context menu on the right click of the mouse.
How to turn off autolisting feature in the code window of C# applications?

It can be done in 2 ways

1)Tools-->Options-->Text Editor-->C#->
uncheck the checkbox with the Text Auto List members


2)Tools-->Options-->Text Editor-->All Languages-->
General-->uncheck the checkbox with the Text Auto List members
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