Great way to earn money!

Zone1

Tuesday, February 28, 2012

Some CSHARP Questions Part 3

Differentiate Finalize(), Dispose() and Destructors
Dispose() is called by as an indication for an object to release any unmanaged resources it has held.

Finalize() is used for the same purpose as dispose however finalize doesn’t assure the garbage collection of an object.

Dispose() operates determinalistically due to which it is generally preferred.


In C#, a destructor and a finalizer refer to the same thing, the function called before the object is fully-removed from the memory (i.e. GC-collected)   and it applies to c sharp only.





class BaseClass {     public ~BaseClass()     {         Console.WriteLine("dtor of BaseClass");     } } class DerivedClass : BaseClass {     public ~DerivedClass()     {         Console.WriteLine("dtor of DerivedClass");     } } class ChildClass : DerivedClass {     public ~ChildClass()     {         Console.WriteLine("dtor of ChildClass");     } }



When you define a class destructor with that C++-alike syntax (preceding the function name with a ~) the compiler actually replaces your destructor with an override of the virtual Object.Finalize() function. 



That is, before the object is removed (i.e. GC-collected) from the memory, the finalizer (i.e. destructor) is called first. 



This finalizer first executes your code. After that it calls the finalizer of the base type of your object. If we could decompile your assembly, would see that our destructor in the ChildClass (so other classes) has been replaced with this function:
protected virtual void Finalize()

{

    try

    {

        Console.WriteLine("dtor of ChildClass");

    }

    finally

    {

        base.Finalize();

    }
}
Finalizers are a feature of GC managed objects (i.e. managed classes). 


That's because the finalizer is called only when the GC removes the object from the memory (i.e. frees memory associated with).

You can't determine when finalizers would be called


That's because you don't know when the next garbage collection would occur, even if you performed a manual garbage collection (using System.GC.Collect() function) you won't know exactly when memory would be released.










Difference between System.String and StringBuilder


Every time we manipulate a string object we get a new string object created.


String is immutable means that once it has been created, a string cannot be changed.

No characters can be added or removed from it, nor can its length be changed.
The String object is immutable while StringBuilder object is mutable.

Both String and StringBuilder are reference type. But String acts like a value type.

StringBuilder is located in the System.Text namespace.
The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object.

Using the StringBuilder class can boost performance when concatenating many strings together in a loop.

Below is a small program:

        private void Example_StringBuilder()

        {
            StringBuilder String1 = new StringBuilder("beautiful");

            StringBuilder String2 = new StringBuilder();

            String2 = String1.Append(" Picture");

            // value of String2="beautiful Picture"

            String2 = String1.Remove(2, 4);

            // value of String2="beful Picture"

            String2 = String1.Insert(2, "auti");

            // value of String2="beautiful Picture"

            String2 = String1.Replace("beautiful", "Wonderful");

            // value of String2="Wonderful Picture"

          }



StringBuilder object contains a buffer, typically initialized with a string but usually larger than that string. 
This buffer can be manipulated in place without creating a new string: You can insert, append, remove, and replace characters. 
When you're done manipulating the characters, use StringBuilder's ToString method to extract the finished string from it. 



Both String and StringBuilder contain Unicode characters of type Char and support an indexer that returns Char. 


Because the String class is immutable, its indexer is read-only, but the StringBuilder indexer is readable/writeable. 


If you modify the string frequently-in operations like reassigning, appending, removing, and replacing some characters-you should choose the StringBuilder class. 


Whenever you modify a string in the String class, the methods of the String class returns a new string as the result. Creating many String objects might degrade the performance of your program. You can avoid creating a new instance of a string by using the StringBuilder class. 



No comments:

Post a Comment