tags:

views:

22

answers:

3

I was reading Linq to Sql. Here I created a dbml file where I found that an autogenerated property has been created

[Column(Storage="_RecursionLevel", DbType="Int")]
public System.Nullable<int> RecursionLevel
{
    get
    {
        return this._RecursionLevel;
    }
    set
    {
        if ((this._RecursionLevel != value))
        {
            this._RecursionLevel = value;
        }
    }
}

Here why if ((this._RecursionLevel != value)) line is written. What is the purpose. Why not directly assigned the value. What benefit they got

A: 

Usually with linq to sql classes (or at least when I used 'em) there's also a propertynotification event fired. Sooo I'm assuming the line firing the event is missing.

Because if the value wasn't changed, no need to change it and thus no need to fire that event.

townsean
A: 

It's basically checking to see whether the value you are attempting to assign to the property is different from what it currently is. It will then only make the assignment if the value is different. Presumably this is an optimisation that saves unnecessary assignments when they are the same and stop any "OnChange" events firing for no good reason.

Dan Diplo
@Dan, townsean: So it means, To save an unnecessary event this check has been used. Assuming if there were no event then was it an optimization. What is the more expansive operation among the two. Assignment or Checking
Shantanu Gupta
That's a good question. I'm not sure which is more expensive, but I'm interested in the answer to that.
townsean
+2  A: 

The garbage collector keeps track of memory regions that are written to since the last GC. If a region is not written to, the garbage collector can skip a lot of checks.

A simple assignment, marks the memory as dirty, even if the memory written contains the same value.

Check this article. Read from "Making Generations Work with Write Barriers" and "Too many object writes".

GvS