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

What does the term immutable mean ?

The data value may not be changed.
Note: The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory.
What is the difference between DirectCast and CType ?

DirectCast requires the object variable to be the same in both the run-time type and the specified type.
If the specified type and the run-time type of the expression are the same, then the run-time performance of DirectCast is better than that of CType.
Ctype works fine if there is a valid conversion defined between the expression and the type.
What is meant by "Death of Diamond" ?

Every object ina a Class is treated as a diamond. In order to recover the memory associated with this object, if the destruction of a class object after it's functionality occurs, then it is known as "Death of Diamond".
What connections does Microsoft SQL Server support ?

Microsoft SQL Server supports trusted and untrusted connections.
Windows Authentication is known as trusted connection because, the username and password are checked with the Active Directory.
SQL Server authentication is untrusted, since SQL Server is the only verifier participating in the transaction.
Which debugging window allows you to see the methods called in the order they were called ?

To view the order of Called method, the only method that is used is Call Stack Window.This window is also useful to see methods called to get to the point at which execution halted.
What does Dispose method do with the connection Object ?

Dipose method is called for an object.
Before the Garbage collector automatically puts the object in to some of its generation, this dispose method is called for the object to release its memory.
What is partial class. & what is advantage ?

Partial class is a keyword which is used to split the defiition of the class into multiple parts with same singature.
When complier compiles the class file ,it makes into single file.

Example:

Public partial class HRModule

{
public void DoWork()
{}
}

Public partial class HRModule
{
public void GoToLunch()
{
}
}

What does the "This window" show in the debugger ?

This window is used to show the Object's instance data.It points to the object that is pointed to by this reference.
What does the parameter Initial Catalog define inside Connection String ?

The parameter Initial Catalog inside the Connection String defines the database name to connect to.
Why strings are immutable ?

A string is a sequential collection of Unicode characters, which is used to represent text. A String, on the other hand, is a .NET Framework class (System.String) that represents a Unicode string with a sequential collection of System.Char structure.
What is the Encapsulation

Encapsulation is a process of binding the data members and member functions into a single unit.
Example for encapsulation is class. A class can contain data structures and methods.
Consider the following class
public class Aperture
{
public Aperture ()
{
}
protected double height;
protected double width;
protected double thickness;
public double get volume()
{
Double volume=height * width * thickness;
if (volume<0)
return 0;
return volume;
}
}
In this example we encapsulate some data such as height, width, thickness and method Get Volume. Other methods or objects can interact with this object through methods that have public access modifier
What is OUT Keyword ?

Out keyword is used for passing a variable for output purpose. It has the same concept of ref keyword. Passing a ref parameter needs variable to be initialized while out parameter is passed without initialized.
It is useful when we want to return more than one value from the method.

Example:

class Test

{
public static void Main()
{
int a; // may be left un-initialized
DoWork(out a); // note out
Console.WriteLine("The value of a is " + a);
}

public static void DoWork(out int i) // note out
{
i=4; //must assigned value.
}
}


The program will result : The value of a is 4
What is the use of "Default" keyword in C# ?

The default keyword has many usage. One of the uses is it returns a type's default value. For reference types it is "null" and for value types it is "zero".

default(Int32) --- prints 0

default(Boolean) --- prints false
default(String) --- null




Thanks and Regards
Akiii
Difference between IEnumerable and IEnumerator Part 1

Before Going for the difference between IEnumerable and IEnumerator , let first discuss what is IEnumerable and IEnumerator is with example. I am going to use the below list

List&lt;string&gt; weekObj = new List &lt;string&gt; ();
weekObj.Add("Sunday");
weekObj.Add("Monday");
weekObj.Add("Tuesday");
weekObj.Add("Wednesday");
weekObj.Add("Thrusday");
weekObj.Add("Friday");
weekObj.Add("Saturday");

IEnumerable

The IEnumerable interface is a generic interface that provides an abstraction for looping over elements.
In addition to providing foreach support, it allows you to tap into the useful extension methods in the System.Linq namespace, opening up a lot of advanced functionality
The IEnumerable interface contains an abstract member function called GetEnumerator() and return an interface IEnumerator on any success call.
Example: Traversing IEnumerable

IEnumerable weekenum = (IEnumerable)weekObj;
foreach (string day in weekenum)
{
Console.WriteLine(day);
}
IEnumerator
IEnumerator provides two abstract methods and a property to pull a particular element in a collection. And they are Reset(), MoveNext() and Current The signature of IEnumerator members is as follows:
void Reset() : Sets the enumerator to its initial position, which is before the first element in the collection.
bool MoveNext() : Advances the enumerator to the next element of the collection.
object Current : Gets the current element in the collection.
Example : Traversing IEnumerator
IEnumerator weekIterator = weekObj.GetEnumerator();

if (weekIterator.MoveNext())
{
Console.WriteLine(weekIterator.Current.ToString());
}

Continue with part 2
Difference between IEnumerable and IEnumerator part-2

In this previous part we have discussed what is IEnumerable and IEnumerator with example.
in this Section we will discuss what are the basic diffrence between these two, and when we need to use this.
List<string> weekObj = new List<string>();
weekObj.Add("Sunday");
weekObj.Add("Monday");
weekObj.Add("Tuesday");
weekObj.Add("Wednesday");
weekObj.Add("Thrusday");
weekObj.Add("Friday");
weekObj.Add("Saturday");
Difference:

1. IEnumerable actually uses IEnumerator. i.e IEnumerable uses IEnumerator internally through GetIEnumerator.
And the Syna

Example:
IEnumerator weekIterator = weekenum.GetEnumerator();
while(weekIterator.MoveNext())
{
Console.WriteLine(weekIterator.Current.ToString());
}
2. The biggest difference is states, i.e. IEnumerable does not remember the cursor state i.e currently row which is iterating through, where I enumerator does.

# Example 1:
Enumerator :
public static void EnumeratorDemo()
{
IEnumerable weekenum = (IEnumerable)weekObj;
IEnumerator weekIterator = weekenum.GetEnumerator();
DisplayDaysSunToWed(weekIterator);
}
static void DisplayDaysSunToWed(IEnumerator obj)
{
while (obj.MoveNext())
{
Console.WriteLine(obj.Current.ToString());
if(obj.Current.Equals("Wednesday"))
{
DisplayDaysThrusToRest(obj);
}
}
}
static void DisplayDaysThrusToRest(IEnumerator obj)
{
while (obj.MoveNext())
{
Console.WriteLine(obj.Current.ToString());
}
}
static void Main(string[] args)
{
IEnuDiffrence.EnumeratorDemo();
Console.Read();
}

The above code will return below result

Sunday
Monday
Tuesday
Wednesday
Thrusday
Friday
Saturday

Continue in part 3
Difference between IEnumerable and IEnumerator part 3

# Example 2 :

IEnumerable:

public static void EnumeratorDemo()
{
IEnumerable weekenum = (IEnumerable)weekObj;
DisplayDaysSunToWed(weekenum);
}
static void DisplayDaysSunToWed(IEnumerator obj)
{
while (obj.MoveNext())
{
Console.WriteLine(obj.Current.ToString());
if(obj.Current.Equals("Wednesday"))
{
DisplayDaysThrusToRest(obj);
}
}
}
static void DisplayDaysSunToWed(IEnumerable obj)
{
foreach (string day in obj)
{
Console.WriteLine(day);
if (day.Equals("Wednesday"))
{
DisplayDaysThrusToRest(obj);
}
}
}
static void DisplayDaysThrusToRest(IEnumerable obj)
{
foreach (string day in obj)
{
Console.WriteLine(day);
}
}
The above code will return below result

Sunday
Monday
Tuesday
Wednesday
Sunday
Monday
Tuesday
Wednesday
Thrusday
Friday
Saturday
Thrusday
Friday
Saturday

Example 2 signifies that IEnumerable is not storing the current cursor state .
Lets debug the example2:

·Main function called DisplayDaysSunToWed(IEnumerable obj) this function prints the below value
Sunday
Monday
Tuesday
Wednesday
· Now the given condition become true 'day.Equals("Wednesday")' , so it will call DisplayDaysThrusToRest(IEnumerable obj) function , now this function will print the below value
Sunday
Monday
Tuesday
Wednesday
Thrusday
Friday
Saturday
· Then it come back to the function 1 ‘DisplayDaysSunToWed()’ , in this function the cursor position is in “Wednesday” , Now this function loop through the rest value mentioned below.

Thrusday
Friday
Saturday
·So the final result is as the above output

Note:
IEnumerable is useful when we have only iterate the value and IEnumerator is useful when we have to pass the iterator as parameter and has to remember the value.
What is Extension Method ?

Extension method is a .net language feature which has introduced to C# in .NET 3.0.
Extension methods are a piece of syntactic sugar which allows a developer to add new methods to the public interface of an existing CLR type without deriving a new class or recompiling the original type.
For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type. “

Rules to create Extension Method?
1.Extension Methods have to be implemented as static methods and in static classes (inside a non-nested, non-generic static class to be more precise).
We need to add the "this" keyword before the first parameter .The "this" keyword in the function declaration instructs the compiler that the function extends the data type that immediately follows it.
2.You aren't actually adding methods to the class, you can only access the public members from your extension method. Logically, any extension methods that are defined on a type are available to any sub classes of that type.
3.For the extension method to be available, you must include the namespace containing the extension.
4.If the type you are extending has a real method that has the same name as the extension method, the extension method is ignored. This is important as it has comparability implications.
5.Extension methods for properties are not possible.
6.Extension methods cannot access private variables in the type they are extending.

A good example of this is testing a integer to see if it is a even number or a odd number . In order to do this,
//Old methodology
public class intHelper

{
public static bool isEven(int input)
{
if (input % 2 == 0)
{
return true;
}
return false;
}
}

// Calling in the desired place like given below .
bool helperRes = intHelper.isEven(20);

// Using extension method
public static class intExtention

{
public static bool isEven(this int input)
{
if (input % 2 == 0)
{
return true;
}
return false;
}
}

// Calling in the desired place like given below .
int inputValue=20;

bool helperRes = inputValue.isEven();

What is CLS-Compliant?

CLSCompliant attribute on an assembly (or a program element) and have the compiler check if your code is CLS (Common Language System) compliant. This means it works properly when consumed by other .NET languages.
How will you call non-abstract methods of an abstract class in derived class?

We can call the methods inside the constructor. For eg:


public class AbstractTest
{
public abstract class AbstractClass
{

public abstract void Method1();

public string GetFullname(string lastname, string firstname)
{
return firstname + lastname;
}
}

public class DerivedClass : AbstractClass
{


public override void Method1()
{
throw new NotImplementedException();
}

public DerivedClass()
{
string fullname = GetFullname("Vijay", "Chakraborty");
//Do further implementation

}

}

What is the difference between proc. sent BY VAL and BY SUB ?

The main difference between these two is mentioned below:

BY VAL: The changes that are made will not be reflected back to the variable.
By REF: The changes will be reflected back to that variable.( same as & symbol in c, c++)
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