FullStackCafe C#I
FullStackCafe C#I
Topics: C# OOP
Answer:
There are some differences between Abstract Class and Interface which are listed below:
1. You expect that unrelated classes would implement your interface. For example,many unrelated objects can
implement Serializable interface.
2. You want to specify the behaviour of a particular data type, but not concerned about who implements its
behaviour.
3. You want to take advantage of multiple inheritance of type.
Q2: What is the difference between ref and out keywords? ☆☆☆
Topics: C#
Answer:
ref tells the compiler that the object is initialized before entering the function, while
out tells the compiler that the object will be initialized inside the function.
Topics: C#
Page 1 of 9
FullStack.Cafe - Kill Your Tech Interview
Answer:
Extension methods enable you to add methods to existing types without creating a new derived type,
recompiling, or otherwise modifying the original type. An extension method is a special kind of static method, but
they are called as if they were instance methods on the extended type.
namespace ExtensionMethods
{
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}
Topics: C#
Answer:
Topics: C#
Answer:
When you do throw ex , that thrown exception becomes the "original" one. So all previous stack trace will not
be there.
If you do throw , the exception just goes down the line and you'll get the full stack trace.
Page 2 of 9
FullStack.Cafe - Kill Your Tech Interview
{
//throw ex resets the stack trace Coming from Method 1 and propogates it to the caller(Main)
throw ex;
}
}
Topics: C#
Answer:
The == Operator (usually means the same as ReferenceEquals , could be overrided) compares the reference
identity while the Equals() ( virtual Equals() ) method compares if two objects are equivalent.
Topics: C#
Answer:
A virtual method is a method that can be redefined in derived classes. A virtual method has an implementation
in a base class as well as derived class. When a virtual method is invoked, the run-time type of the object is
checked for an overriding member.
Topics: C#
Answer:
The reason for the using statement is to ensure that the object is disposed (call IDisposable ) as soon as it
goes out of scope, and it doesn't require explicit code to ensure that this happens.
Page 3 of 9
FullStack.Cafe - Kill Your Tech Interview
to:
Topics: C#
Answer:
Anonymous types allow us to create a new type without defining them. This is way to defining read only
properties into a single object without having to define type explicitly. Here Type is generating by the compiler
and it is accessible only for the current block of code. The type of properties is also inferred by the compiler.
Consider:
Topics: C#
Answer:
Reflection is the ability to query and interact with the type system in a dynamic way. Generally speaking
Reflection allows you access to metadata about objects. For instance you can load a DLL and determine if it
contains an implementation of an interface. You could use this to discover dll's that support functionality at
runtime. Use could use this to extend an application without a recompilation and without having to restart it.
Page 4 of 9
FullStack.Cafe - Kill Your Tech Interview
Topics: C#
Answer:
having to declare the value at the time of a definition for a const VS readonly values can be computed
dynamically but need to be assigned before the constructor exits.. after that it is frozen.
'const's are implicitly static . You use a ClassName.ConstantName notation to access them.
AssemblyB references AssemblyA and uses these values in code. When this is compiled,
in the case of the const value, it is like a find-replace, the value 2 is 'baked into' the AssemblyB 's IL. This
means that if tomorrow I'll update I_CONST_VALUE to 20 in the future. AssemblyB would still have 2 till I
recompile it.
in the case of the readonly value, it is like a ref to a memory location. The value is not baked into
AssemblyB 's IL. This means that if the memory location is updated, AssemblyB gets the new value without
recompilation. So if I_RO_VALUE is updated to 30, you only need to build AssemblyA . All clients do not need to
be recompiled.
Remember: If you reference a constant from another assembly, its value will be compiled right into the calling
assembly. That way when you update the constant in the referenced assembly it won't change in the calling
assembly!
Q12: Why can't you specify the accessibility modifier for methods
inside the Interface? ☆☆☆
Topics: C#
Answer:
In an interface, we have virtual methods that do not have method definition. All the methods are there to be
overridden in the derived class. That's why they all are public.
Topics: C#
Answer:
Page 5 of 9
FullStack.Cafe - Kill Your Tech Interview
Topics: C# OOP
Answer:
A Virtual method must always have a default implementation. However, it can be overridden in the derived
class, though not mandatory. It can be overridden using override keyword.
An Abstract method does not have an implementation. It resides in the abstract class. It is mandatory that
the derived class implements the abstract method. An override keyword is not necessary here though it can
be used.
Topics: C#
Answer:
A Destructor is used to clean up the memory and free the resources. But in C# this is done by the garbage
collector on its own. System.GC.Collect() is called internally for cleaning up. The answer to second question is
"almost never".
Typically one only creates a destructor when your class is holding on to some expensive unmanaged resource
that must be cleaned up when the object goes away. It is better to use the disposable pattern to ensure that
the resource is cleaned up. A destructor is then essentially an assurance that if the consumer of your object
forgets to dispose it, the resource still gets cleaned up eventually.
If you make a destructor be extremely careful and understand how the garbage collector works. Destructors are
really weird:
They don't run on your thread; they run on their own thread. Don't cause deadlocks!
An unhandled exception thrown from a destructor is bad news. It's on its own thread; who is going to catch
it?
A destructor may be called on an object after the constructor starts but before the constructor finishes. A
properly written destructor will not rely on invariants established in the constructor.
A destructor can "resurrect" an object, making a dead object alive again. That's really weird. Don't do it.
A destructor might never run; you can't rely on the object ever being scheduled for finalization. It probably
will be, but that's not a guarantee.
Topics: C#
Answer:
Page 6 of 9
FullStack.Cafe - Kill Your Tech Interview
The object type is an alias for System.Object in .NET. In the unified type system of C#, all types, predefined
and user-defined, reference types and value types, inherit directly or indirectly from System.Object . You can
assign values of any type to variables of type object.
The dynamic type indicates that use of the variable and references to its members bypass compile-time
type checking. Instead, these operations are resolved at run time. The dynamic type simplifies access to
COM APIs such as the Office Automation APIs, to dynamic APIs such as IronPython libraries, and to the HTML
Document Object Model (DOM).
Type dynamic behaves like type object in most circumstances. In particular, any non-null expression can be
converted to the dynamic type. The dynamic type differs from object in that operations that contain
expressions of type dynamic are not resolved or type checked by the compiler. The compiler packages
together information about the operation, and that information is later used to evaluate the operation at run
time. As part of the process, variables of type dynamic are compiled into variables of type object. Therefore,
type dynamic exists only at compile time, not at run time.
Object: the following code has compile error unless cast object to string:
dynamic: the following code compiles successfully but if it contains a value except string it throws Runtime
error
Topics: C#
Answer:
Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility
of a class member.
Public access specifier allows a class to expose its member variables and member functions to other
functions and objects. Any public member can be accessed from outside the class.
Page 7 of 9
FullStack.Cafe - Kill Your Tech Interview
Private access specifier allows a class to hide its member variables and member functions from other
functions and objects. Only functions of the same class can access its private members. Even an instance of
a class cannot access its private members.
Protected access specifier allows a child class to access the member variables and member functions of its
base class. This way it helps in implementing inheritance.
Topics: C#
Answer:
Internal access specifier allows a class to expose its member variables and member functions to other functions
and objects in the current assembly. In other words, any member with internal access specifier can be accessed
from any class or method defined within the application in which the member is defined.
You can use it for utility or helper classes/methods that you would like to access from many other classes within
the same assembly, but that you want to ensure code in other assemblies can't access.
Topics: C#
Answer:
A lambda expression is an anonymous function that you can use to create delegates or expression tree types.
By using lambda expressions, you can write local functions that can be passed as arguments or returned as the
value of function calls. Lambda expressions are particularly helpful for writing LINQ query expressions.
In the following example, the lambda expression x => x * x , which specifies a parameter that's named x and
returns the value of x squared, is assigned to a variable of a delegate type:
Topics: C#
Answer:
An anonymous function is an "inline" statement or expression that can be used wherever a delegate type is
expected. You can use it to initialize a named delegate or pass it instead of a named delegate type as a method
parameter.
Page 8 of 9
FullStack.Cafe - Kill Your Tech Interview
Lambda Expressions
Anonymous Methods
Consider:
Page 9 of 9