ObjectIdGenerator class in C#

Introduction :-

ObjectIdGenerator is a class which is present under System.Runtime.Serialization namespace.This class play an important role while we checking the object at runtime.

Background:-

Before telling all about this class; let me first tell you why we going to use this class ?
what was it’s benefit ?
So to know about any thing we always ask what was it’s benefit ?
Right ?
The benefit of this class is it will helps you to know that whether the object is created or not and is it created for first time or it’s their for a time period
Second benefit is that it provide you the information at run time that whether the object hasID or not.

This class bacially contain two method such as :-
1. GetId()
2. HasId()

GetId() method will help you to provide the Id for the object at run time.
HasId() method will return the Id if the Id exist of particular object other wise returns 0.
Syntax:-
GetId(object obj,Output bool flag);
HasId(object obj,Output bool flag);

Both GetId() and HasId() takes two parameter.
First parameter say about your object for which you want to know the Id.
Second parameter is out parameter and this will gives you Boolean value either true or false.

Let’s have a Demo on this :-

class StringBuilderDemo
    {
       static void Main()
        {
            string str = "My first string is :This is My First Program";
            bool bFlag;
            string result = string.Empty;
            ObjectIDGenerator obj = new ObjectIDGenerator();
            Console.WriteLine("{0} and New Id is {1}",str, obj.GetId(str, out bFlag));
            result = bFlag ? "Yes" : "No";
            Console.WriteLine("Is this a New Instance ? Ans:-{0}", result);

            str += "Hello world";
            Console.WriteLine("{0} and New Id is {1}", str, obj.GetId(str, out bFlag));
            result = bFlag ? "Yes" : "No";
            Console.WriteLine("Is this a New Instance ? Ans:-{0}", result);

            StringBuilder objString = new StringBuilder("This is My Secoond String :- My Second program is");
            Console.WriteLine("{0} and New Id Is {1}",objString,obj.GetId(objString,out bFlag));
            result = bFlag ? "Yes" : "No";
            Console.WriteLine("Is this a New Instance ? Ans:-{0}", result);

            objString.Append("Addition of Two Number");

            Console.WriteLine("{0} and New Id is {1}", objString, obj.GetId(objString, out bFlag));
            result = bFlag ? "Yes" : "No";
            Console.WriteLine("Is this a New Instance ? Ans:-{0}", result);

            //Checking the stiring object using HasId method 
            Console.WriteLine("This has id {0}",obj.HasId(objString,out bFlag));
            string objstr = string.Empty;
            Console.WriteLine("This has id {0}", obj.HasId(objstr, out bFlag));

        }
    }

OutPut :-

objectinstancedemo

Explanation :-
In this demo i used string and stringbuilder.
This demo is a best example for to prove how string is immutable in nature and stringbuilder is mutable in nature.

As we know string is immutable and stringbulider is mutable.
So what i did above I first declare a string type of variable and assign some value and printing the Id of that string variable by using Getid() method inside Console.WritteLine() (See above demo carefully). It returns id value and whether it’s object or not ?
And when we assign a new value in that variable by using “+=” then we see that it create a new object id while i am printing again by using GetId() it provide new Id value. That’s prove string is Immutable in nature.
Where as when i did same thing using string builder and again printing it by using getId() then it’s not given new Id and that proves string builder is mutable in nature.

We always confusing the question(why string is immutable and stringbulider is mutable ?) in interview.
So be confident about this which is mutable and which is immutable and why ?

My strongly suggestion to you try  this example once you get the better Idea.

Hope this make you clear picture about the ObjectIdGenerator Class and it’s uses .
Thanks.
Please have your comment in comment box.

#Share#Like#Comment

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s