What is Managed or Unmanaged Code?
- Managed Code – The code, which is developed in .NET framework is known as managed code. This code is directly executed by CLR with the help of managed code execution. Any language that is written in .NET Framework is managed code.
- Unmanaged Code – The code, which is developed outside .NET framework is known as unmanaged code. Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C++ can be used to write such applications, which, for example, access low – level functions of the operating system. Background compatibility with the code of VB, ASP and COM are examples of unmanaged code.
What is Boxing and Unboxing?
Boxing and Unboxing both are used for type conversion but have some difference:
- Boxing – Boxing is the process of converting a value type data type to the object or to any interface data type which is implemented by this value type. When the CLR boxes a value means when CLR is converting a value type to Object Type, it wraps the value inside a System.Object and stores it on the heap area in application domain.
- Unboxing – Unboxing is also a process which is used to extract the value type from the object or any implemented interface type. Boxing may be done implicitly, but unboxing have to be explicit by code.
The concept of boxing and unboxing underlines the C# unified view of the type system in which a value of any type can be treated as an object.
What is the difference between a struct and a class in C#? What is the difference between a struct and a class in C#?
C# 112 JuniorAnswer
Class and struct both are the user defined data type but have some major difference:
** Struct**
- The struct is value type in C# and it inherits from System.Value Type.
- Struct is usually used for smaller amounts of data.
- Struct can’t be inherited to other type.
- A structure can’t be abstract.
- No need to create object by new keyword.
- Do not have permission to create any default constructor.
Class
- The class is reference type in C# and it inherits from the System.Object Type.
- Classes are usually used for large amounts of data.
- Classes can be inherited to other class.
- A class can be abstract type.
- We can’t use an object of a class with using new keyword.
- We can create a default constructor.
Q Explain Anonymous type in C#
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:
var anonymousData = new
{
ForeName = "Jignesh",
SurName = "Trivedi"
};
Console.WriteLine("First Name : " + anonymousData.ForeName);
Q: What is difference between constants and readonly?
Constant variables are declared and initialized at compile time. The value can’t be changed afterwards. Readonly is used only when we want to assign the value at run time.
A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const keyword and must be initialized as they are declared.
public class MyClass
{
public const double PI1 = 3.14159;
}
A readonly member is like a constant in that it represents an unchanging value. The difference is that a readonly member can be initialized at runtime, in a constructor, as well being able to be initialized as they are declared.
public class MyClass1
{
public readonly double PI2 = 3.14159;
//or
public readonly double PI3;
public MyClass2()
{
PI3 = 3.14159;
}
}
const
- They can not be declared as
static(they are implicitly static) - The value of constant is evaluated at compile time
- constants are initialized at declaration only
readonly
- They can be either instance-level or static
- The value is evaluated at run time
- Readonly can be initialized in declaration or by code in the constructorQU
Q : When to use Task.Delay, when to use Thread.Sleep?
Use Thread.Sleep when you want to block the current thread.
Use Task.Delay when you want a logical delay without blocking the current thread.
Efficiency should not be a paramount concern with these methods. Their primary real-world use is as retry timers for I/O operations, which are on the order of seconds rather than milliseconds.
Q: output of below program
{
private static string result;
public static void Main()
{
MainAsync().GetAwaiter().GetResult();
}
static async Task MainAsync()
{
await SaySomething();
youConsole.WriteLine(result);
}
}
Answer: Helloworld
public class TestStatic {
public static int TestValue;
public TestStatic() {
if (TestValue == 0) {
TestValue = 5;
}
}
static TestStatic() {
if (TestValue == 0) {
TestValue = 10;
}
}
public void Print() {
if (TestValue == 5) {
TestValue = 6;
}
Console.WriteLine("TestValue : " + TestValue);
}
}
public void Main(string[] args) {
TestStatic t = new TestStatic();
t.Print();
}
Answer: output 10.
Q:What is the “yield” keyword used for in C#?
IEnumerable<object> FilteredList()
{
foreach( object item in FullList )
{
if( IsItemInPartialList( item )
yield return item;
}
}
yield will return every item from for-each every time condition is satisfied.
Q:What is jagged array in C#.Net and when to prefer jagged arrays over multi-dimensional arrays?
Bear in mind, that the CLR is heavily optimized for single-dimension array access, so using a jagged array will likely be faster than a multidimensional array of the same size.
What are the benefits of a Deferred Execution in LINQ?
In LINQ, queries have two different behaviors of execution: immediate and deferred. Deferred execution means that the evaluation of an expression is delayed until its realized value is actually required. It greatly improves performance by avoiding unnecessary execution.
Consider:
var results = collection.Select(item => item.Foo).Where(foo => foo < 3).ToList();
With deferred execution, the above iterates your collection one time, and each time an item is requested during the iteration, performs the map operation, filters, then uses the results to build the list.
If you were to make LINQ fully execute each time, each operation (Select / Where) would have to iterate through the entire sequence. This would make chained operations very inefficient.
What is MSIL?
When we compile our .NET code then it is not directly converted to native/binary code; it is first converted into intermediate code known as MSIL code which is then interpreted by the CLR. MSIL is independent of hardware and the operating system. Cross language relationships are possible since MSIL is the same for all .NET languages. MSIL is further converted into native code.
Q:Explain how does Asynchronous tasks (Async/Await) work in .NET?
Q:Explain Finalize vs Dispose usage?
Finalize is the backstop method, called by the garbage collector when it reclaims an object. Dispose is the “deterministic cleanup” method, called by applications to release valuable native resources (window handles, database connections, etc.) when they are no longer needed, rather than leaving them held indefinitely until the GC gets round to the object.
As the user of an object, you always use Dispose. Finalize is for the GC.
As the implementer of a class, if you hold managed resources that ought to be disposed, you implement Dispose. If you hold native resources, you implement both Dispose and Finalize, and both call a common method that releases the native resources. These idioms are typically combined through a private Dispose(bool disposing) method, which Dispose calls with true, and Finalize calls with false. This method always frees native resources, then checks the disposing parameter, and if it is true it disposes managed resources and calls GC.SuppressFinalize.
Q: Explain the differences between “out” and “ref” parameters in C#?
Although quite similar, the “out” parameter can be passed to a method and doesn’t require to be initialised whereas the “ref” parameter has to be initialised and set to something before it is used
Q:What is marshalling and why do we need it?
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.
Why to use lock statement in C#?
Lock will make sure one thread will not intercept the other thread which is running the part of code. So lock statement will make the thread wait, block till the object is being released.