0% found this document useful (0 votes)
56 views

FullStackCafe C#II

The document contains questions and answers about various C# concepts. [1] It discusses how to catch multiple exceptions at once without code duplication using a try/catch block that checks for specific exception types. [2] It explains the primary use of the IDisposable interface is to clean up unmanaged resources and when cleanup occurs depends on how Dispose is called. [3] It compares Tasks and Threads in .NET, noting that tasks do not create OS threads and allow determining completion and returning results.

Uploaded by

abhi78612345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

FullStackCafe C#II

The document contains questions and answers about various C# concepts. [1] It discusses how to catch multiple exceptions at once without code duplication using a try/catch block that checks for specific exception types. [2] It explains the primary use of the IDisposable interface is to clean up unmanaged resources and when cleanup occurs depends on how Dispose is called. [3] It compares Tasks and Threads in .NET, noting that tasks do not create OS threads and allow determining completion and returning results.

Uploaded by

abhi78612345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

FullStack.

Cafe - Kill Your Tech Interview

FullStack.Cafe - Kill Your Tech Interview

Q1: Is there a way to catch multiple exceptions at once and


without code duplication? ☆☆☆

Topics: .NET Core C#

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:

Catch System.Exception and switch on the types:

catch (Exception ex)


{
if (ex is FormatException || ex is OverflowException)
{
WebId = Guid.Empty;
return;
}

throw;
}

Q2: Why to use of the IDisposable interface? ☆☆☆

Topics: .NET Core C#

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

Topics: .NET Core C#

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.

Q4: What is the difference between overloading and overriding?


☆☆☆

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
}
}

public class test2 : test


{
public override void getStuff(int id)
{
//base.getStuff(id);
//or - Get stuff new location
}
}

Q5: What is the use of Null Coalescing Operator ( ?? ) in C#? ☆☆☆

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.

string name = null;


string myname = name ?? "Laxmi";
Console.WriteLine(myname);

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.

Q7: What is Marshalling and why do we need it? ☆☆☆☆

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.

Q8: What is difference between late binding and early binding in


C#? ☆☆☆☆

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.

Q9: What is the difference between is and as operators in C#?


☆☆☆☆

Topics: C#

Page 3 of 7
FullStack.Cafe - Kill Your Tech Interview

Answer:

The is operator checks if an object can be cast to a specific type.

if (someObject is StringBuilder) ...

The as operator attempts to cast an object to a specific type, and returns null if it fails.

StringBuilder b = someObject as StringBuilder;


if (b != null) ...

Q10: What are the different ways a method can be overloaded?


☆☆☆☆

Topics: C#

Answer:

Methods can be overloaded using different data types for parameter, different order of parameters, and different
number of parameters.

Q11: What are pointer types in C#? ☆☆☆☆

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++.

char* cptr; int* iptr;

Q12: What is scope of a Protected Internal member variable of a


C# class? ☆☆☆☆

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.

Q13: Can you create a function in C# which can accept varying


number of arguments? ☆☆☆☆

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.

public static void UseParams(params int[] list) {


for (int i = 0; i < list.Length; i++) {
Console.Write(list[i] + " ");
}
Console.WriteLine();
}

public static void UseParams2(params object[] list) {


for (int i = 0; i < list.Length; i++) {
Console.Write(list[i] + " ");
}
Console.WriteLine();
}

// usage
UseParams(1, 2, 3, 4);
UseParams2(1, 'a', "test");

Q14: Is operator overloading supported in C#? ☆☆☆☆

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.

public static Box operator+ (Box b, Box c) {


Box box = new Box();
box.length = b.length + c.length;
box.breadth = b.breadth + c.breadth;
box.height = b.height + c.height;
return box;
}

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.

Q15: What is the difference between System.ApplicationException


class and System.SystemException class? ☆☆☆☆

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

Q16: Why to use lock statement in C#? ☆☆☆☆

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:

private static readonly Object obj = new Object();

lock (obj)
{
// critical section
}

Q17: Can Multiple Inheritance implemented in C# ? ☆☆☆☆

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.

Q18: What is the yield keyword used for in C#? ☆☆☆☆

Topics: C#

Problem:

Consider:

IEnumerable<object> FilteredList()
{
foreach( object item in FullList )
{
if( IsItemInPartialList( item )
yield return item;
}
}

Could you explain what does the yield keyword do there?

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

Yield has two great uses:

It helps to provide custom iteration without creating temp collections.


It helps to do stateful iteration.

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:

public void Consumer()


{
foreach(int i in Integers())
{
Console.WriteLine(i.ToString());
}
}

public IEnumerable<int> Integers()


{
yield return 1;
yield return 2;
yield return 4;
yield return 8;
yield return 16;
yield return 16777216;
}

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.

Q19: What interface should your data structure implement to


make the Where method work? ☆☆☆☆

Topics: C#

Answer:

Implementing IEnumerable makes using foreach and where possible.

Q20: IEnumerable vs List - What to Use? How do they work? ☆☆☆☆

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

You might also like