How does the generational garbage collector in the
.NET CLR manage object lifetime? What is non-deterministic finalization?
The hugely simplistic version is that every time it garbage-collects,
it starts by assuming everything to be garbage, then goes through and builds a
list of everything reachable. Those become not-garbage, everything else doesn’t,
and gets thrown away. What makes it generational is that every time an object
goes through this process and survives, it is noted as being a member of an
older generation (up to 2, right now). When the garbage-collector is trying to
free memory, it starts with the lowest generation (0) and only works up to
higher ones if it can’t free up enough space, on the grounds that shorter-lived
objects are more likely to have been freed than longer-lived ones.
Non-deterministic finalization implies that the destructor (if any) of an object
will not necessarily be run (nor its memory cleaned up, but that’s a relatively
minor issue) immediately upon its going out of scope. Instead, it will wait
until first the garbage collector gets around to finding it, and then the
finalisation queue empties down to it; and if the process ends before this
happens, it may not be finalised at all. (Although the operating system will
usually clean up any process-external resources left open - note the usually
there, especially as the exceptions tend to hurt a lot.)
What is the difference
between Finalize() and Dispose()?
Dispose() is called by the user of an object to indicate that he is
finished with it, enabling that object to release any unmanaged resources it
holds. Finalize() is called by the run-time to allow an object which has not had
Dispose() called on it to do the same. However, Dispose() operates
determinalistically, whereas there is no guarantee that Finalize() will be
called immediately when an object goes out of scope - or indeed at all, if the
program ends before that object is GCed - and as such Dispose() is generally
preferred.
How is the using()
pattern useful? What is IDisposable? How does it support deterministic
finalization?
The using() pattern is useful because it ensures that Dispose() will
always be called when a disposable object (defined as one that implements
IDisposable, and thus the Dispose() method) goes out of scope, even if it does
so by an exception being thrown, and thus that resources are always released.
What does this useful
command line do? tasklist /m “mscor*”
Lists all the applications and associated tasks/process currently running
on the system with a module whose name begins “mscor” loaded into them; which in
nearly all cases means “all the .NET processes”.
What’s wrong with a line
like this? DateTime.Parse(myString);
Therez nothing wrong with this declaration.Converts the specified
string representation of a date and time to its DateTime equivalent.But If the
string is not a valid DateTime,It throws an exception.
What are PDBs? Where
must they be located for debugging to work?
A program database (PDB) files holds debugging and project state information
that allows incremental linking of debug configuration of your program.There are
several different types of symbolic debugging information. The default type for
Microsoft compiler is the so-called PDB file. The compiler setting for creating
this file is /Zi, or /ZI for C/C++(which creates a PDB file with additional
information that enables a feature called “”Edit and Continue”") or a Visual
Basic/C#/JScript .NET program with /debug.
A PDB file is a separate file,
placed by default in the Debug project subdirectory, that has the same name as
the executable file with the extension .pdb. Note that the Visual C++ compiler
by default creates an additional PDB file called VC60.pdb for VisulaC++6.0 and
VC70.PDB file for VisulaC++7.0. The compiler creates this file during
compilation of the source code, when the compiler isn’t aware of the final name
of the executable. The linker can merge this temporary PDB file into the main
one if you tell it to, but it won’t do it by default. The PDB file can be useful
to display the detailed stack trace with source files and line numbers.
What is FullTrust? Do
GAC’ed assemblies have FullTrust?
Before the .NET Framework existed, Windows had two levels of trust for
downloaded code. This old was a binary trust . You only had two
choices: Full Trust, and No Trust. The code could either do anything you could
do, or it wouldn’t run at all.
The permission sets in .NET include FullTrust, SkipVerification, ,
Nothing, LocalIntranet, Internet and Everything. Full Trust Grants unrestricted
permissions to system resources. Fully trusted code run by a normal,
nonprivileged user cannot do administrative tasks, but can access any resources
the user can access, and do anything the user can do. From a security
standpoint, you can think of fully trusted code as being similar to native,
unmanaged code, like a traditional ActiveX control.
GAC assemblies are granted FullTrust. In v1.0 and 1.1, the fact that assemblies
in the GAC seem to always get a FullTrust grant is actually a side effect of the
fact that the GAC lives on the local machine. If anyone were to lock down
the security policy by changing the grant set of the local machine to something
less than FullTrust, and if your assembly did not get extra permission from some
other code group, it would no longer have FullTrust even though it lives in the
GAC.
What does this do?
gacutil /l | find /i “Corillian”
The Global Assembly Cache tool allows you to view and manipulate the contents of
the global assembly cache and download cache.The tool comes with various
optional params to do that.
“”/l”" option Lists the contents of the global assembly cache. If you specify
the assemblyName parameter(/l [assemblyName]), the tool lists only the
assemblies matching that name.
What does this do .. sn
-t foo.dll ?
Sn -t option displays the token for the public key stored in infile.
The contents of infile must be previously generated using -p.
Sn.exe computes the token using a hash function from the public key. To save
space, the common language runtime stores public key tokens in the manifest as
part of a reference to another assembly when it records a dependency to an
assembly that has a strong name. The -tp option displays the public key in
addition to the token.
How do you generate a
strong name?
.NET provides an utility called strong name tool. You can run this toolfrom the
VS.NET command prompt to generate a strong name with an option “-k” and
providing the strong key file name. i.e. sn- -k < file-name >
What is the difference between a
Debug and Release build? Is there a significant speed difference? Why or why
not?
The Debug build is the program compiled with full symbolic debug information and
no optimization. The Release build is the program compiled employing
optimization and contains no symbolic debug information. These settings can be
changed as per need from Project Configuration properties. The release runs
faster since it does not have any debug symbols and is optimized.
Explain the use of
virtual, sealed, override, and abstract.
Abstract: The keyword can be applied for a class or method.
1. Class: If we use abstract keyword for a class it makes the
class an abstract class, which means it cant be instantiated. Though
it is not nessacary to make all the method within the abstract class to be
virtual. ie, Abstract class can have concrete methods
2. Method: If we make a method as abstract, we dont need to provide
implementation
of the method in the class but the derived class need to implement/override this
method.
Sealed: It can be applied on a class and methods. It stops the type from further
derivation i.e no one can derive class
from a sealed class,ie A sealed class cannot be inherited.A sealed class cannot
be a abstract class.A compile time error is thrown if you try to specify sealed
class as a base class.
When an instance method declaration includes a sealed modifier, that method is
said to be a sealed method. If an instance method declaration includes the
sealed modifier, it must also include the override modifier. Use of the sealed
modifier prevents a derived class from further overriding the method For
Egs: sealed override public void Sample() { Console.WriteLine(”Sealed Method”);
}
Virtual & Override: Virtual &
Override keywords provides runtime polymorphism. A base class can make some of
its methods
as virtual which allows the derived class a chance to override the base class
implementation by using override keyword.
For e.g. class Shape
{
int a
public virtual void Display()
{
Console.WriteLine(”Shape”);
}
}
class Rectangle:Shape
{
public override void Display()
{
Console.WriteLine(”Derived”);
}
}
Explain the importance and use of each, Version, Culture and
PublicKeyToken for an assembly.
This three alongwith name of the assembly provide a strong name or
fully qualified name to the assembly. When a assebly is referenced with all
three.
PublicKeyToken: Each assembly
can have a public key embedded in its manifest that identifies the developer.
This ensures that once the assembly ships, no one can modify the code or other
resources contained in the assembly.
Culture: Specifies which culture the assembly supports
Version: The version number of the assembly.It is of the following form
major.minor.build.revision.
Explain the differences between
public, protected, private and internal.
These all are access modifier and they governs the access level. They can be
applied to class, methods, fields.
Public: Allows class, methods, fields to be accessible from anywhere i.e. within
and outside an assembly.
Private: When applied to field and method allows to be accessible within a
class.
Protected: Similar to private but can be accessed by members of derived class
also.
Internal: They are public within the assembly i.e. they can be accessed by
anyone within an assembly but outside assembly they are not visible.
What is the difference
between typeof(foo) and myFoo.GetType()?
Typeof is operator which applied to a object returns System.Type object. Typeof
cannot be overloaded white GetType has lot of overloads.GetType is a method
which also returns System.Type of an object. GetType is used to get the runtime
type of the object.
Example from MSDN showing
Gettype used to retrive type at untime:-
public class MyBaseClass: Object
{
}
public class MyDerivedClass:
MyBaseClass {
}
public class Test {
public static void
Main() {
MyBaseClass myBase = new MyBaseClass();
MyDerivedClass myDerived = new MyDerivedClass();
object o = myDerived;
MyBaseClass b = myDerived;
Console.WriteLine(”mybase: Type is {0}”, myBase.GetType());
Console.WriteLine(”myDerived: Type is {0}”,
myDerived.GetType());
Console.WriteLine(”object o = myDerived: Type is {0}”,
o.GetType());
Console.WriteLine(”MyBaseClass b = myDerived: Type is
{0}”, b.GetType());
}
}
/*
This code produces the following
output.
mybase: Type is MyBaseClass
myDerived: Type is MyDerivedClass
object o = myDerived: Type is MyDerivedClass
MyBaseClass b = myDerived: Type is MyDerivedClass
*/
Can “this” be used
within a static method?
No ‘This’ cannot be used in a static method. As only static
variables/methods can be used in a static method.