FullStackCafe C#II
FullStackCafe C#II
Problem:
Consider:
try
{
WebId = new Guid(queryString["web"]);
}
catch (FormatException)
{
WebId = Guid.Empty;
}
catch (OverflowException)
{
WebId = Guid.Empty;
}
Is there a way to catch both exceptions and only call the WebId = Guid.Empty call once?
Solution:
throw;
}
Answer:
The "primary" use of the IDisposable interface is to clean up unmanaged resources. Note the purpose of the
Dispose pattern is to provide a mechanism to clean up both managed and unmanaged resources and when that
occurs depends on how the Dispose method is being called.
Q3: Explain the difference between Task and Thread in .NET ☆☆☆
Page 1 of 7
FullStack.Cafe - Kill Your Tech Interview
Answer:
Thread represents an actual OS-level thread, with its own stack and kernel resources. Thread allows the
highest degree of control; you can Abort() or Suspend() or Resume() a thread, you can observe its state,
and you can set thread-level properties like the stack size, apartment state, or culture. ThreadPool is a
wrapper around a pool of threads maintained by the CLR.
The Task class from the Task Parallel Library offers the best of both worlds. Like the ThreadPool , a task does
not create its own OS thread. Instead, tasks are executed by a TaskScheduler ; the default scheduler simply
runs on the ThreadPool. Unlike the ThreadPool, Task also allows you to find out when it finishes, and (via the
generic Task) to return a result.
Topics: C#
Answer:
Overloading is when you have multiple methods in the same scope, with the same name but different
signatures.
//Overloading
public class test
{
public void getStuff(int id)
{}
public void getStuff(string name)
{}
}
Overriding is a principle that allows you to change the functionality of a method in a child class.
//Overriding
public class test
{
public virtual void getStuff(int id)
{
//Get stuff default location
}
}
Topics: C#
Page 2 of 7
FullStack.Cafe - Kill Your Tech Interview
Answer:
The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value
types or reference types. It returns the left-hand operand if the operand is not null; otherwise, it returns the right
operand.
Q6: How can you prevent a class from overriding in C#? ☆☆☆
Topics: OOP C#
Answer:
You can prevent a class from overriding in C# by using the sealed keyword.
Topics: C#
Answer:
Because different languages and environments have different calling conventions, different layout conventions,
different sizes of primitives (cf. char in C# and char in C), different object creation/destruction conventions, and
different design guidelines. You need a way to get the stuff out of managed land an into somewhere where
unmanaged land can see and understand it and vice versa. That's what marshalling is for.
Marshaling is the process between managed code and unmanaged code; It is one of the most important
services offered by the CLR.
Topics: C#
Answer:
In Compile time polymorphism or Early Binding we will use multiple methods with same name but
different type of parameter or may be the number or parameter because of this we can perform different-
different tasks with same method name in the same class which is also known as Method overloading.
Run time polymorphism also known as late binding, in Run Time polymorphism or Late Binding we can
do use same method names with same signatures means same type or same number of parameters but not
in same class because compiler doesn't allowed that at compile time so we can use in derived class that bind
at run time when a child class or derived class object will instantiated that's way we says that Late Binding
also known as Method overriding.
Topics: C#
Page 3 of 7
FullStack.Cafe - Kill Your Tech Interview
Answer:
The as operator attempts to cast an object to a specific type, and returns null if it fails.
Topics: C#
Answer:
Methods can be overloaded using different data types for parameter, different order of parameters, and different
number of parameters.
Topics: C#
Answer:
Pointer type variables store the memory address of another type. Pointers in C# have the same capabilities as
the pointers in C or C++.
Topics: C#
Answer:
The protected internal access specifier allows a class to hide its member variables and member functions from
other class objects and functions, except a child class within the same application. This is also used while
implementing inheritance.
Topics: C#
Page 4 of 7
FullStack.Cafe - Kill Your Tech Interview
Answer:
By using the params keyword, you can specify a method parameter that takes a variable number of arguments.
No additional parameters are permitted after the params keyword in a method declaration, and only one params
keyword is permitted in a method declaration. The declared type of the params parameter must be a single-
dimensional array.
// usage
UseParams(1, 2, 3, 4);
UseParams2(1, 'a', "test");
Topics: C#
Answer:
A user-defined type can overload a predefined C# operator. That is, a type can provide the custom
implementation of an operation in case one or both of the operands are of that type.
The above function implements the addition operator ( + ) for a user-defined class Box . It adds the attributes of
two Box objects and returns the resultant Box object.
Topics: C#
Answer:
The System.ApplicationException class supports exceptions generated by application programs. Hence the
exceptions defined by the programmers should derive from this class.
The System.SystemException class is the base class for all predefined system exception.
Page 5 of 7
FullStack.Cafe - Kill Your Tech Interview
Topics: C#
Answer:
The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the
critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released.
The lock keyword calls Enter at the start of the block and Exit at the end of the block. lock keyword actually
handles Monitor class at back end.
For example:
lock (obj)
{
// critical section
}
Topics: C#
Answer:
In C#, derived classes can inherit from one base class only. If you want to inherit from multiple base classes, use
interface.
Topics: C#
Problem:
Consider:
IEnumerable<object> FilteredList()
{
foreach( object item in FullList )
{
if( IsItemInPartialList( item )
yield return item;
}
}
Solution:
The function returns an object that implements the IEnumerable interface. If a calling function starts foreach-ing
over this object the function is called again until it "yields". This is syntactic sugar introduced in C# 2.0.
Page 6 of 7
FullStack.Cafe - Kill Your Tech Interview
The advantage of using yield is that if the function consuming your data simply needs the first item of the
collection, the rest of the items won't be created.
Another example:
When you step through the example you'll find the first call to Integers() returns 1. The second call returns 2 and
the line "yield return 1" is not executed again.
Topics: C#
Answer:
Topics: C#
Answer:
IEnumerable describes behavior, while List is an implementation of that behavior. When you use IEnumerable ,
you give the compiler a chance to defer work until later, possibly optimizing along the way. If you use ToList()
you force the compiler to reify the results right away.
Whenever you "stacking" LINQ expressions, you should use IEnumerable , because by only specifying the
behavior gives LINQ a chance to defer evaluation and possibly optimize the program.
Page 7 of 7