Some Interview Questions: C#

It is also permissible to declare multiple variables of the same underlying type on a single line of code, as in the following three bool variables
bool b1 = true, b2 = false, b3 = b1;

All intrinsic data types support what is known as a default constructor. This feature allows you to create a variable using the new keyword, which automatically sets the variable to its default value
bool b = new bool();              // Set to false.
  int i = new int();                // Set to 0.
  double d = new double();          // Set to 0.
  DateTime dt = new DateTime();     // Set to 1/1/0001 12:00:00 AM

All the data types have been derived from System.Object class.  The value types are the types that have System.ValueType in their chain of inheritance.  Usually types related to numbers will be the value types
int is a short hand notation for System.Int32. It is a value type.
string is a short hand notation for System.String. It is a reference type

When you prefix a string literal with the @ symbol, you have created what is termed a verbatim string. Using verbatim strings, you disable the processing of a literal's escape characters and print out a string as is.

A reference type is an object allocated on the garbage-collected managed heap. By default, when you perform a test for equality on reference types (via the C# == and ! = operators), you will be returned true if the references are pointing to the same object in memory. However, even though the string data type is indeed a reference type, the equality operators have been redefined to compare the values of string objects, not the object in memory to which they refer.
Widening is the term used to define an implicit upward cast that does not result in a loss of data.
All narrowing conversions result in a compiler error, even when you can reason that the narrowing conversion should indeed succeed
When you wrap a statement (or a block of statements) within the scope of the checked keyword, the C# compiler emits additional CIL instructions that test for overflow conditions that may result when adding, multiplying, subtracting, or dividing two numerical data types. If an overflow has occurred, you will receive a runtime exception: System.OverflowException
1)  byte sum = checked((byte)Add(b1, b2));

2)  checked
        {byte sum = (byte)Add(b1, b2);
        Console.WriteLine("sum = {0}", sum);}
 
C# checked and unchecked keywords, remember that the default behavior of the .NET runtime is to ignore arithmetic overflow/underflow. When you want to selectively handle discrete statements, make use of the checked keyword. If you wish to trap overflow errors throughout your application, enable the /checked flag. Finally, the unchecked keyword may be used if you have a block of code where overflow is acceptable (and thus should not trigger a runtime exception).
 
Implicit typing applies only to local variables in a method or property scope. It is illegal to use the var keyword to define return values, parameters, or field data of a custom type.
 
class ThisWillNeverCompile
{
  // Error! var cannot be used as field data!
  private var myInt = 10;
 
  // Error! var cannot be used as a return value
  // or parameter type!
  public var MyMethod(var x, var y){}
// Error! Must assign a value!
var myData;
 
// Error! Must assign value at exact time of declaration!
var myInt;
myInt = 0;
 
// Error! Can't assign null as initial value!
var myObj = null;
 
// Also OK!
var myInt = 0;
var anotherInt = myInt;
 
string myString = "Wake up!";
var myData = myString;
 
}
 
 
·         If a reference type is passed by reference, the callee may change the values of the object's state data, as well as the object it is referencing.
·         If a reference type is passed by value, the callee may change the values of the object's state data, but not the object it is referencing.
By default, when a value type contains other reference types, assignment results in a copy of the references. In this way, you have two independent structures, each of which contains a reference pointing to the same object in memory (i.e., a shallow copy). When you want to perform a deep copy, where the state of internal references is fully copied into a new object, one approach is to implement the ICloneable interface




Intriguing Question
Value Type
Reference Type
Where are objects allocated?
Allocated on the stack.
Allocated on the managed heap.
How is a variable represented?
Value type variables are local copies.
Reference type variables are pointing to the memory occupied by the allocated instance.
What is the base type?
Implicitly extends System.ValueType.
Can derive from any other type (except System.ValueType), as long as that type is not "sealed" (more details on this in Chapter 6).
Can this type function as a base to other types?
No. Value types are always sealed and cannot be inherited from.
Yes. If the type is not sealed, it may function as a base to other types.
What is the default parameter passing behavior?
Variables are passed by value (i.e., a copy of the variable is passed into the called function).
For value types, the object is copied-by-value. For reference types, the reference is copied-by-value.
Can this type override System.Object.Finalize()?
No. Value types are never placed onto the heap and, therefore, do not need to be finalized.
Yes
Can I define constructors for this type?
Yes, but the default constructor is reserved (i.e., your custom constructors must all have arguments).
But, of course!
When do variables of this type die?
When they fall out of the defining scope.
When the object is garbage collected.

C# ?? operator allows you to assign a value to a nullable type if the retrieved value is in fact null. For this example, assume you want to assign a local nullable integer to 100 if the value returned from GetIntFromDatabase() is null
a class is a user-defined type that is composed of field data (often called member variables) and members that operate on this data (such as constructors, properties, methods, events, and so forth). Collectively, the set of field data represents the "state" of a class instance (otherwise known as an object).


How Garbage collector works
How dispose() method works
Why C# is a purely OO language
What are the differences b/w virtual and abstract methods?
Abstract method must be implemented in child class. Virtual method may or may not be overridden in child class.
Abstract method must not have a definition. Virtual method must have a definition
Abstract method must be declared in an abstract class. Virtual method can be declared anywhere.
How to break inheritance?
With Method Hiding.
How overloading works with default parameters?
What is method hiding? How it will be used in C#?
Method hiding will be used to hide the base class’s method to implement child class’s own logic with base class’s method name. We need new keyword before the method name in child class.
What is encapsulation and abstraction?
Types of polymorphism..

why do we create private constructors..
Print 
1
1 2 1
1 2 3 2 1

what are different directives in angular JS...
How to use dependency injection without a container
Create a class model for below
StoreTimings
Day StartTime EndTime
Monday 9 6
Tuesday 9 6
...
Sunday 9 5

Class model for chess game..
why do we use readonly and const
Can we use destructors in C#?
how to implement security for resources in REST based APIs
what is the difference between thread and process..
ACID implementation in transaction processing..

difference between non-action methods and private methods for controller class





Please add your comments if it helps....

Comments

Popular posts from this blog

Base 64 encoding and decoding

LINQ Queries with GROUP BY, INNER JOIN, COUNT and SUM: Examples

How to write Custom delete Confirmation Modal for Kendo Grid in MVC: