Thursday, December 6, 2007

C#.net Inv Questions.

What is Code Access Security (CAS)?

Code access security is used to ensure that access to protected resources
and operations is allowed only if the security policy allows it.

To use CAS, the code should be verifiably type-safe code - i.e., code that can be verified by the JIT compiler to be type-safe (note that in some cases, the code may be type-safe, but the JIT compiler cannot verify it to be type-safe). Type-safe code accesses only the memory locations it is authorized to access, and only in well-defined, allowable ways.

At runtime, the .NET security system ensures controlled access to protected resources and operations by walking the call stack - each caller in the stack must have the permissions being demanded for the operation being performed. If not, the operation fails and an exception is thrown.

For more on code access security and related topics refer to:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconintroductiontocodeaccesssecurity.asp

Compare declarative versus imperative security

Declarative security is implemented via attributes. No explicit code is required to implement this.

Imperative security is implemented by explicitly coding the security requirements. An instance of the permission object is created and security calls are issued.



What is the difference between Demand and Assert?

Demand - calling code can access the resource protected by a permission demand through the code that calls this method, only if callers higher in the stack have been granted permission to access the resource. Results in a stack walk.

Assert - calling code can access the resource protected by a permission demand through the code that calls this method, even if callers higher in the stack have not been granted permission to access the resource. Does not result in a stack walk.

What is the difference between Demand and LinkDemand.

• Demand specifies that code access security stack walk must occur and all callers on the stack must have the permission or identity to pass. Demand occurs on every call at runtime.
• LinkDemand happens only at just-in-time (JIT) compilation time and checks only the immediate caller. It does not check the caller's caller.

What is an Application Domain?
An application domain represents isolation/scoping unit for a .NET
application. .NET allows multiple applications to be loaded in a single process. It achieves this by loading each application in its own independent application domain.

Each application domain is isolated from all other application domains. You can think of application domains as lightweight processes with a process. This is more efficient than creating a separate process for each application and provides the same benefit (isolation).

Use System.AppDomain to create application domains programmatically. CLR itself runs in an application domain called the default domain.

How do you handle errors in VB.NET and C#?

C# and VB.NET use structured error handling (unlike VB6 and earlier versions where error handling was implemented using Goto statement). Error handling in both VB.NET and C# is implemented using Try..Catch..Finally construct (C# uses lower case construct – try...catch...finally).

What is the purpose of the finally block?

The code in finally block is guaranteed to run, irrespective of whether an error occurs or not. Critical portions of code, for example release of file handles or database connections, should be placed in the finally block.

What is the difference between a static and an instance constructor?

An instance constructor implements code to initialize the instance of the class. A static constructor implements code to initialize the class itself when it is first loaded.

What is a virtual method?

In C#, virtual keyword can be used to mark a property or method to make it overrideable. Such methods/properties are called virtual methods/properties.

By default, methods and properties in C# are non-virtual.

Assume that a class, Class1, has both instance and static constructors. Given the code below, how many times will the static and instance constructors fire?

Class1 c1 = new Class1();
Class1 c2 = new Class1();
Class1 c3 = new Class1();

By definition, a static constructor is fired only once when the class is loaded. An instance constructor on the other hand is fired each time the class is instantiated. So, in the code given above, the static constructor will fire once and the instance constructor will fire three times.

You create a class but did not define any instance constructor. Which of the following statements is correct?

A. Since no instance constructor was explicitly defined, the code will not compile and give an error.
B. The code will compile but will give a runtime error.
C. The compiler will automatically provide an instance constructor with empty body and no parameters.

Statement C - The compiler will automatically provide an instance constructor with empty body and no parameters is correct.

Can you inherit from multiple base classes in C#?

No. C# does not support multiple inheritance, so you cannot inherit from more than one base class. You can however, implement multiple interfaces.

No comments: