Interview Tips Interview Tips, Interview Questions and Answers

29Nov/100

Understanding Reference Types

Reference type variables are named appropriately (reference) because the variable holds a reference to an object. In C and C++, you have something similar that is called a pointer, which points to an object. While you can modify a pointer, you can't modify the value of a reference - it simply points at the object in memory.

An important fact you need to understand is that when you are assigning one reference type variable to another, only the reference is copied, not the object. The variable holds the reference and that is what is being copied. Listing 22-2 shows how this works.
// Reference Type Assignment

using System;

class Employee
{
private string m_name;

public string Name
{
get { return m_name; }
set { m_name = value; }
}
}

class Program
{
static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";

Employee bob = new Employee();
bob.Name = "Bob";

Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);

// assign joe reference to bob variable
bob = joe;

Console.WriteLine();
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);

joe.Name = "Bobbi Jo";

Console.WriteLine();
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);

Console.ReadKey();
}
}

Here's the output:

Original Employee Values:
joe = Joe
bob = Bob

Values After Reference Assignment:
joe = Joe
bob = Joe

Values After Changing One Instance:
joe = Bobbi Jo
bob = Bobbi Jo

3Mar/100

C# interview questions and answers

I need to restrict a class by creating only one object throughout the application. How can I achieve this?
We can declare the constructor of the class as either protected or as private

int? d = 1; Type testType = d.GetType(); will result…
NOTE: This is objective type question, Please click question title for correct answer.

How can you make your machine shutdown from your program?
Process.Start("shutdown", "-s -f -t 0");

What is the use of unsafe keyword in C#?
In C# the value can be directly referenced to a variable, so there is no need of pointer. Use of pointer sometime crashes the application. But C# supports pointer, that means we can use pointer in C#.

The use of pointer in C# is defined as a unsafe code. So if we want to use pointer in C# the pointer must be present in the body of unsafe declaration. But pointer does not come under garbage collection.

Example:-

unsafe
{
int a, *b;
a = 25;
b = &a;
Console.WriteLine("b= {0}",b);//returns b= 25
}

What is difference between var and Dynamic ?
Var word was introduced with C#3.5(specifically for LINQ) while dynamic is introduced in C#4.0. variables declared with var keyword will get compiled and you can have all its related methods by intellisense while variables declared with dynamic keyword will never get compiled. All the exceptions related to dynamic type variables can only be caught at runtime.

What is dynamic keyword ?
Its newly introduced keyword of C#4.0. To indicate that all operations will be performed runtime.

What will happen if you declare a variable named "checked" with any data type?
Compiler will throw an error as checked is a keyword in C# So It cannot be used as variable name. Checked keyword is used to check the overflow arithmetic checking.

What are anonymous methods?
Anonymous methods are another way to declare delegates with inline code except named methods.

Restrictions of yield in try-catch.
While using yield keyword, mainly two restrictions are observed.
First is , we can’t use yield in finally.
Second is , we can’t place yield keyword in the catch block if try contains more than one catch blocks.

With yield break statement, control gets …
NOTE: This is objective type question, Please click question title for correct answer.

What first action compiler will take on detection of iterator ?
As soon as compiler will detect iterator, it will automatically generate current, MoveNext and Disposemethods of the IEnumerator or IEnumerator(T) type.

6Jan/100

10 Frequently asked C# questions, Class Questions

    1. What is the syntax to inherit from a class in C#?
      Place a colon and then the name of the base class.
      Example: class MyNewClass : MyBaseClass
    2. What is an interface class?
      Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes. 
    3. Why can’t you specify the accessibility modifier for methods inside the interface?
      They all must be public, and are therefore public by default.
    4. What’s an abstract class?
      A class that cannot be instantiated. An abstract class is a class that must be inherited and have the methods overridden. An abstract class is essentially a blueprint for a class without any implementation.
    5. When do you absolutely have to declare a class as abstract?
      1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.
      2. When at least one of the methods in the class is abstract.
    6. Can you prevent your class from being inherited by another class?
      Yes. The keyword “sealed” will prevent the class from being inherited. 
    7. What is the difference between a Struct and a Class?
      Structs are value-type variables and are thus saved on the stack, additional overhead but faster retrieval.  Another difference is that structs cannot inherit.
    8. Can you allow a class to be inherited, but prevent the method from being over-ridden?
      Yes. Just leave the class public and make the method sealed.
    9. What’s the difference between an interface and abstract class?
      In an interface class, all methods are abstract - there is no implementation. In an abstract class some methods can be concrete. In an interface class, no accessibility modifiers are allowed.  An abstract class may have accessibility modifiers.
    10. Can you inherit multiple interfaces?
      Yes.  .NET does support multiple interfaces.