Anonymous Types in C#

Hi Friends,

In today’s discussion, we’ll see anonymous types. Well by books, Anonymous types are strongly typed, immutable with a bunch of properties. we basically create the instance of anonymous types by basically providing properties value like shown below.

so, I have created a var person and created an instance the next thing is anonymous
initializer, and this is created a whole new class behind the scenes. In order to inspect the same what you can do is inspect the same in Reflector using Ildasm. It will show you a bunch of scary things out there. but, for this discussion, am keeping the things fairly simple and just focusing on the high level things.
var person= new { FirstName = “Rahul”, LastName = “Sahay” };

now, there are certain catch with the same, If i try to change the order of the same,
person = new { LastName = “scott”,FirstName = “dave” }; // will popup error

i cannot do that because, the 1st one which i created has got the same in reference like below

let me just show you the var indication.

anony1st

 

Now, when i run the same it will print the below output like below:

anony2nd

 

so, having said to my earlier statement anonymous types are strongly typed. you will get fairly good intellisense while writing the code as shown below.

anony3rd

also, one more point when i mentioned that anonymous types are immutable. this can be referred as below like

anony4th

 

so, it’s says that it’s a readonly property cannot be intervened again, once initialized.

now, we have liberty of having embedded anonymous types as well like shown below

anony5th

 

now, when i run the same, it will print the below string inside braces obviously.

anony6th

 

However, this is ok when i have to print one variable like person kind of thing,how about
when i have to take care of bunch of values, so in this case i need to consider Implicitly Typed Arrays like shown below

anony7th

obviously, since i have to loop through the array, so i have changed the printing condition and now, the output would go like this

anony8th

 

So, now with this i would like to wrap this discussion. Till then, stay tuned and Happy Coding.

Thanks,
Rahul
Happy Coding

1,303 total views, no views today

Posted in C#

View State Improvements in ASP.Net 4.0

Hi friends,

Today, in this discussion, i would like to stress upon View State improvements which is done in ASP.Net 4.0 series. So, it says basically, that view state can be disabled completely @ page level, but it can be enabled @ control level. so, it means you can disable the same @ top level and re-enable the same control level selectively. Initially, it was like disable view state or enable view state, means all or nothing.

so, let’s get started, let’s explain the same visual studio itself.

1st

so, here i have added 2 things, a simple label just to track the no of page loads and one table just to make the view state size huge.

2nd

 

so, when i do every time postback, it will increment the value by 1. so, viewstate is basically a mechanism for remembering the control state.

3rd 4th 5th 6th

 

so, as you see upon submitting that counter gets incremented, but when i saw the page source, i could see the real mess, here the thing, is on every subsequent request viewstate of the page is getting incremented and hence will cause page to load slow. this chunk is basically base 64 encoding which is basically view state encoding mechanism. so, basically what we used to do @ 1st attempt to disable the viewstate @ the page level likewise

7th

 

and that would certainly solve the problem as this will disable completely viewstate from the page and page gets optimized, bang we achieved to the solution. but, we certainly didn’t want this. we wanted to keep track of no of clicks, how would we achieve then, hmmm if you try doing enableViewstate=”True” @ control level, that would not work. so, what we would do instead, we would use ViewStateMode property as shown below.

8th

now, this will solve the problem. Now, new view state will look like this

9th

so, with this i would like to wrap, till then stay tuned and happy coding.

Thanks,
Rahul

1,711 total views, 1 views today