ThreadStatic in C#


In c# language Threadstatic is used when we work with threading. Threadstatic is a keyword which is used to declare static resource.

What is static resources ?

Static resource means the resource is specific to each thread.
For example:- You have two thread and if both the thread using same resource at that time the resource not sharable it specific to each thread.

Background :-
Let’s have a Demo using static thread :-

class ThreadStatic
        static int _iField;
        public static void Main()
            Thread t = new Thread(new ThreadStart(() =>
                  for (int i = 0; i < 10; i++) { _iField++; Console.WriteLine("Thread is {0}",_iField); } })); t.Start(); Thread t1 = new Thread(new ThreadStart(() =>
                for (int i = 0; i < 10; i++)
                    Console.WriteLine("Second thread is {0}", _iField);


Explanation :- In the above example we have use Threadstatic keyword for _ifield variable so when we executing two different thread then each thread have a copy of _iField variable; due to this reason our output is 1-10 in each thread.

But if i remove the ThreadStatic form the _ifiled variable then we get the out put as 1-20. See bellow :-


Hope you got Now !!!



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.


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.
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 :-


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 .
Please have your comment in comment box.


ForEach() method in .Net


Why i write about this ?
 When i find this ForEach() then i thought it’s same as “foreach” loop and just skipping it but when i see that it has method symbol then bit confused and started reading about this.At last i found that both are different. The ForEach() is a method specifically designed to iterate over list or collection type of data; and this method present under List class inside base class library.

public void ForEach(Action action);
When to use  ?
This is a method present under List class inside base class library; this method basically used when you working with entity framework and when you are using list to iterate over it.
How to use ?
It can only be used for the list type of data.
for eg:- List lstStudent;

It takes a delegate as parameter; where we need to pass the lambda expression and access all the list data.

Demo :-

var enrollments = new List()
new Enrollment { CourseId=1000, StudentId=1, grade=Grade.A },
new Enrollment { CourseId=1001,StudentId=2, grade=Grade.B },
new Enrollment {CourseId=1002, StudentId=3, grade=Grade.C },
new Enrollment {CourseId=1000, StudentId=4, grade=Grade.D },
new Enrollment {CourseId=1003, StudentId=5, grade=Grade.B },
new Enrollment {CourseId=1004, StudentId=6,grade=Grade.A },
enrollments.ForEach(e => context.Enrollments.Add(e));

Explanation :- Here I have enrollment list and i am adding the value inside Enrollment list by using ForEach() method.In the above ‘e’ represent lambda expression and what i am doing here is that i am iterating over this particular list enrollments and adding each value of enrollment list to my context class Enrollment list.
Context is my class where the Enrollments list present; see bellow:-

class context
List Enrollments;
If any doubt or any question regarding the above explanation feel free to ask me inside comment box.
#Share #Comment #Like


Pointer-Event in HTML5

Pointer-Event is a styling attribute which helps you to disable any HTML control.

For eg:-

    <meta name="viewport" content="width=device-width" />
   <textarea>    </textarea>     <button type="button" onclick="Clicked()" style="pointer-events:none">Click</button> </body> </html>

When to Use :-
So while you developing any web apps and you want to make the button to disable;that means if you want to disable all the event for particular HTML element then we can use this attribute Pointer-Event inside style and set the property value as none so that it will not going to fire any event.

You can also set many more values for this attribute.
Play with this by using this all attribute values.

You can find all details about this attribute Here

Happy Coding 🙂
“Follow me by clicking on right side follow icon to get latest update immediately.”