Miscrosoft.NET Advanced Question Answers

8/31/2012 No Comment

Miscrosoft.NET Advanced Question Answers with code.

Can we use static constructors to initialize non static members?
Yes, it is possible. But we have to create an object of the class inside the static constructor and then initialize the non static member through the object reference.

Code(example):
class Class2
{
int a;
static Class2()
{
Class2 p = new Class2();
p.a = 45;
System.Console.WriteLine(p.a);
}
static void Main()
{
}
}
Is MSIL a high/low/middle level language?

MSIL is Microsoft Intermediate Language:
It is nothing but a set of CPU independent instructions which represent your code and used to provide language interoperability and compatibility of your code on different platforms.

It is not a high level language:
High Level language is C#:
High level language  as such is more human readable,closer to a spoken language.
(Example: programming constructs like loops,if else conditions, keywords like using, class)
High level language defines modular programming,OOPS concepts and the source code.

MSIL is not low level language either:
Low level language is in an executable or binary format(eg: machine language)
MSIL is not in an executable or binary format.

MSIL is not middle level language too:
Middle level language is C. It has modular programming and source code.
It is human readable.
C also supports low level programming (Assembly Language).

What is the difference between Assembly.LoadFrom() and Assembly.LoadFile() methods?
Both methods are used to load the assemblies. we can then extract all the metatdata of the assembly using Reflection.

Difference:
1)LoadFrom can use either the name or the path of the assembly, LoadFile will expect the path of the assembly(see the overloaded versions)

2)LoadFrom uses a probing algorithm to find the assembly.f if you have two assemblies that have the same identity but different locations, you can get some unexpected behavior.But using LoadFile, you can load the desired assembly as needed.

Can you install multiple assemblies together?
Answer : Yes, we can install multiple assemblies together use installutil command in .NET
Ex: installutil Assembly1.exe Assembly2.exe.
The above command will install both the Assemblies in a transactional manner. the two are dependent ie if installation of one of the assemblies fail, the installation of the other assembly will be rolled back.

What is the difference between lock and Monitor.Enter() in .NET ?
Answer : The lock keyword in .NET basically provides a shortcut to Enter method of Monitor class.
Monitor is used to provide thread synchronization. It means till the Thread in which the method is being used finishes its task, no other thread can access the same object.

example:
lock (object)
{
}
It is compiled into
Monitor.Enter(object);
try
{
//code
}
finally
{
Monitor.Exit(object);
}
We can write much more code and perform customization in the try block.

Dear readers, please use the comment section for suggestions, feedback and also for any questions that you have for us.

No comments :

 

Aired | The content is copyrighted and may not be reproduced on other websites. | Copyright © 2009-2016 | All Rights Reserved 2016

Contact Us | About Us | Privacy Policy and Disclaimer