I hate technical books that try to sound young...
-
Uhm I am confused, there are no anonymous types used in either code snippet! :confused:
Regards, Nish
Latest article: Code Project Posts Analyzer for Windows Phone 7 My technology blog: voidnish.wordpress.com
-
I guess you did not read the other responses in this thread. :rolleyes:
Regards, Nish
Latest article: Code Project Posts Analyzer for Windows Phone 7 My technology blog: voidnish.wordpress.com
-
I guess you did not read the other responses in this thread. :rolleyes:
Regards, Nish
Latest article: Code Project Posts Analyzer for Windows Phone 7 My technology blog: voidnish.wordpress.com
-
Quite a few of them. ;)
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
Yeah, the number of C# developers who don't understand the var keyword is shocking!
Regards, Nish
Latest article: Code Project Posts Analyzer for Windows Phone 7 My technology blog: voidnish.wordpress.com
-
I decided it was time I read up on LINQ: never really used it, time I learnt. So, I have a quick look for a book. Amazon: Sams LINQ Unleashed for C#. Lets have a quick look "Surprise me" "check out Wikipedia; Wikipedia is wicked cool at providing detailed facts" Hmmm. Wicked cool? And a code fragment to illustrate how wonderfull anonymous types are:
var request = HttpWebRequest.Create(string.Format(url, stock)); using (var response = request.GetResponse()) { using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII)) { return (reader.ReadToEnd()); } }
I hate that. Why use an example where it is actually clearer if you don't use anonymous types?
WebRequest request = HttpWebRequest.Create(string.Format(url, stock)); using (WebResponse response = request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII)) { return (reader.ReadToEnd()); } }
Don't think I'll buy that one...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
var is used in LINQ extensively so you do not have to create a new class for each LINQ statement that returns a subset of properties from the object being queried(or a new combination from several objects) , e.g.
var result = from employee in employees
select new { employee.Id, employee.Name}This way you don't have to create a new class containing only Id and Name, but you still get the benefit of strongly-typed LINQ result, i.e. you can do
and
We can all discuss about benefit of var vs. declaring a variable type explicitly (i.e. var dataReader against SqlDataReader dataReader) till the cows come home and won't go anywhere. I think the benefit of var in this case is very small (if any), and if people don't want to use var for this I'm fine about it.
"A democracy is nothing more than mob rule, where fifty-one percent of the people may take away the rights of the other forty-nine." - Thomas Jefferson "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." - Benjamin Franklin Edbert Sydney, Australia
-
var is used in LINQ extensively so you do not have to create a new class for each LINQ statement that returns a subset of properties from the object being queried(or a new combination from several objects) , e.g.
var result = from employee in employees
select new { employee.Id, employee.Name}This way you don't have to create a new class containing only Id and Name, but you still get the benefit of strongly-typed LINQ result, i.e. you can do
and
We can all discuss about benefit of var vs. declaring a variable type explicitly (i.e. var dataReader against SqlDataReader dataReader) till the cows come home and won't go anywhere. I think the benefit of var in this case is very small (if any), and if people don't want to use var for this I'm fine about it.
"A democracy is nothing more than mob rule, where fifty-one percent of the people may take away the rights of the other forty-nine." - Thomas Jefferson "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." - Benjamin Franklin Edbert Sydney, Australia
This is a good answer.
-
Abhinav S wrote:
It makes the varible implicity typed i.e. the variable knows nothing about what it will be until the code exectues.
That's not fully correct. The variable type is known at compile time. It just saves you some typing :-)
Regards, Nish
Latest article: Code Project Posts Analyzer for Windows Phone 7 My technology blog: voidnish.wordpress.com
Nishant Sivakumar wrote:
Abhinav S wrote: It makes the varible implicity typed i.e. the variable knows nothing about what it will be until the code exectues. That's not fully correct. The variable type is known at compile time
Yeah, for that in .net 4, the
dynamic
keyworkd was introduced. And I have a bad feeling about it, I think this will give me a lot of headache in the future, specially when the "give me more codezzzz plzzzzz" learn about it. -
I decided it was time I read up on LINQ: never really used it, time I learnt. So, I have a quick look for a book. Amazon: Sams LINQ Unleashed for C#. Lets have a quick look "Surprise me" "check out Wikipedia; Wikipedia is wicked cool at providing detailed facts" Hmmm. Wicked cool? And a code fragment to illustrate how wonderfull anonymous types are:
var request = HttpWebRequest.Create(string.Format(url, stock)); using (var response = request.GetResponse()) { using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII)) { return (reader.ReadToEnd()); } }
I hate that. Why use an example where it is actually clearer if you don't use anonymous types?
WebRequest request = HttpWebRequest.Create(string.Format(url, stock)); using (WebResponse response = request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII)) { return (reader.ReadToEnd()); } }
Don't think I'll buy that one...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
I found the best book is "Linq in Action" - see here: Click[^]. You can then download LinqPad[^] and all the examples from the book are already loaded. Fast track to learning Linq imo.
It’s not because things are difficult that we do not dare, it’s because we do not dare that things are difficult. ~Seneca
-
OriginalGriff wrote:
Technically the use of "var" in the first example makes it an anonymous type, albeit a simplistic one.
Well in this case the var is just an implicitly typed shortcut and the type is known at compile time. I would not call this an anonymous type at all. But I get what you mean.
Regards, Nish
Latest article: Code Project Posts Analyzer for Windows Phone 7 My technology blog: voidnish.wordpress.com
That's just a question of how long it remains anonymous ;)
-
I found the best book is "Linq in Action" - see here: Click[^]. You can then download LinqPad[^] and all the examples from the book are already loaded. Fast track to learning Linq imo.
It’s not because things are difficult that we do not dare, it’s because we do not dare that things are difficult. ~Seneca
Thanks - I'll have a look. LinqPad (at a brief glance) looks like it may be worth downloading anyway.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."