.NET 2.0 features
-
I'm interested ( in light of the post below ) to know how many people have taken to the new 2.0 features ? We all use typed containers, I am sure, but how often do you use an anonymous method or iterators ? As I said on that thread, I used my first anonymous method, because it struck me as the ideal solution as I worked on it, before it's never seemed appropriate. I wonder if I'm just not in the habit yet, or this was truly the first time they were the right tool for the job. I love the idea of iterators, but I don't think I've used them in production code yet, and I imagine I need to look out for chances to change my mindset to be using them. How about you ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Generics, anonymous methods, yield, DynamicMethod, static and partial classes, separate visibility levels for property accessors, delegate co/contravariance, Marshal.GetDelegateForFunctionPointer(), debugger visualizers - I've used all of those and more. .NET 2.0 is :cool: !
-
Generics, anonymous methods, yield, DynamicMethod, static and partial classes, separate visibility levels for property accessors, delegate co/contravariance, Marshal.GetDelegateForFunctionPointer(), debugger visualizers - I've used all of those and more. .NET 2.0 is :cool: !
J. Dunlap wrote:
delegate co/contravariance
What's that?
J. Dunlap wrote:
Marshal.GetDelegateForFunctionPointer(),
When do you use that?
J. Dunlap wrote:
debugger visualizers
Are these things you've written, or using the visualizers already in the debugger?
J. Dunlap wrote:
separate visibility levels for property accessors
Ah, I forgot about that!
J. Dunlap wrote:
DynamicMethod
Say, do you have those dynamic property setters folded into MyXaml yet? ;P Marc
Some people believe what the bible says. Literally. At least [with Wikipedia] you have the chance to correct the wiki -- Jörgen Sigvardsson
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer -
J. Dunlap wrote:
delegate co/contravariance
What's that?
J. Dunlap wrote:
Marshal.GetDelegateForFunctionPointer(),
When do you use that?
J. Dunlap wrote:
debugger visualizers
Are these things you've written, or using the visualizers already in the debugger?
J. Dunlap wrote:
separate visibility levels for property accessors
Ah, I forgot about that!
J. Dunlap wrote:
DynamicMethod
Say, do you have those dynamic property setters folded into MyXaml yet? ;P Marc
Some people believe what the bible says. Literally. At least [with Wikipedia] you have the chance to correct the wiki -- Jörgen Sigvardsson
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmerMarc Clifton wrote:
What's that?
Co/contravariance is where the return type or argument types of the method that a delegate wraps don't have to be the exact same types as declared in the delegate signature. The return type on the method can be a type derived from the delegate's return type, and the arguments can be a less specific type than those declared in the delegate signature.
//Covariance:
public delegate Control CovarianceExampleDelegate;public class CovarianceExample
{
Form GetForm()
{
...
}void Example()
{
//CovarianceExampleDelegate's declared type is Control, but we can create an instance of it
//based on the GetForm() method because GetForm's return type is derived from
//CovarianceExampleDelegate's return type
CovarianceExampleDelegate d = GetForm;
}
}//Contravariance:
public delegate void ContravarianceExampleDelegate(string s);
public class ContravarianceExample
{
void Add(object o)
{
//...
}
void Example()
{
//Add()'s argument type is 'object' rather than 'string', but this works because 'string' is
//derived from 'object'
ContravarianceExampleDelegate d = Add;
}
}Marc Clifton wrote:
When do you use that? [Marshal.GetDelegateForFunctionPointer()]
I used it to dynamically call a method in a C-style DLL without declaring the P/Invoke sig at compile time. You can also do things like execute raw machine code.
Marc Clifton wrote:
Are these things you've written, or using the visualizers already in the debugger?
I wrote a GraphicsPath visualizer and a Bitmap visualizer.
Marc Clifton wrote:
Say, do you have those dynamic property setters folded
-
My hopes in .Net 2.0 was Generics BUT... I wished create some type like MyBox Now I wish a collection of MyBox objects, but I can't. I can have only a collection of MyBox, MyBox, etc, but not of "MyBox" :doh: Another problem is the ?? operator and the Nullable notation (int?, string?, etc). How I can have problems using these together, I ever use Nullable instead... I will use iterators when I have nothing more important to do... The only thing what I use and abuse is the provider model (membership, sitemap, etc). But have the problem what is developed to read configurations of the web.config. If you wish use in Windows Forms, will be needed a lot of work to read from app.config (I have mentioned, what even if you use class libraries, you need referencing things like System.Web.Configuration, Syste.Web.Security, etc) This is my points and rants :) -- modified at 19:39 Monday 14th August, 2006 Jesus is Love! Tell to someone! :-)
click.ok wrote:
MyBox objects
If generics are anything like C++ templates, then MyBox is not a type, and can therefore not have any instances. Generics/templates are in essence copy'n'paste mechanisms requiring no actual copy'n'paste. :)
-- Torn from tomorrow's headlines
-
Christian Graus wrote:
but how often do you use an anonymous method or iterators
I'm not sure what "good code practices" are for anonymous methods, I just figure that it would be for a line or two of code, and this is the first time I've had something that met that requirement. As to iterators, I have yet to write my own iterator or type the word "yield". That's probably because of the nature of the code I write, rather than whatever kind of code you write that would leverage iterators. I use partial classes a lot, because it keeps the source file smaller. And typed containers, like you said, are everywhere. Marc
Some people believe what the bible says. Literally. At least [with Wikipedia] you have the chance to correct the wiki -- Jörgen Sigvardsson
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmerMarc Clifton wrote:
As to iterators, I have yet to write my own iterator or type the word "yield". That's probably because of the nature of the code I write, rather than whatever kind of code you write that would leverage iterators.
I haven't written a line of code using the yield stuff. But from what I can tell, it does have potential for clever patterns. Do you know if yield can handle non-linear collections such as dictionaries?
-- Made From Meat By-Products
-
click.ok wrote:
MyBox objects
If generics are anything like C++ templates, then MyBox is not a type, and can therefore not have any instances. Generics/templates are in essence copy'n'paste mechanisms requiring no actual copy'n'paste. :)
-- Torn from tomorrow's headlines
Generics are more than just copy-and-paste mechanisms - they are special "incomplete" types whose "type slots" are filled in dynamically at JIT time with the types specified in code that uses specific instances of them. When people use a specific version of a generic type in their code, what it is bound against is an actual compiled type in the assembly, with all the metadata, versioning, and other features of a normal type. You can even use reflection to create a specific instance of a generic type at runtime without having to specify the type arguments at compile time. This makes generics ideal for dynamic code gen and dynamic binding purposes.
-
Generics are more than just copy-and-paste mechanisms - they are special "incomplete" types whose "type slots" are filled in dynamically at JIT time with the types specified in code that uses specific instances of them. When people use a specific version of a generic type in their code, what it is bound against is an actual compiled type in the assembly, with all the metadata, versioning, and other features of a normal type. You can even use reflection to create a specific instance of a generic type at runtime without having to specify the type arguments at compile time. This makes generics ideal for dynamic code gen and dynamic binding purposes.
-
Generics are more than just copy-and-paste mechanisms - they are special "incomplete" types whose "type slots" are filled in dynamically at JIT time with the types specified in code that uses specific instances of them. When people use a specific version of a generic type in their code, what it is bound against is an actual compiled type in the assembly, with all the metadata, versioning, and other features of a normal type. You can even use reflection to create a specific instance of a generic type at runtime without having to specify the type arguments at compile time. This makes generics ideal for dynamic code gen and dynamic binding purposes.
Still copy'n'paste in my book. :) List == IntList, List == StringList. Semantically, wherever you can put a List, you can put an IntList. The obvious benefit with the generics is that you don't have tio write all the methods (Append(), Delete(), etc) for each and every type you want to use. Ok, so I'll meet you halfway; it's copy'n'link for you, but copy'n'paste for the compiler/linker/JIT (it still has to "copy" for each new instantiation of the generic). :)
-- [LIVE] From Omicron Persei 8
-
"I'm unmanagable"
-- Now with chucklelin
I like to know when the garbageman comes
-
Generics, obviously. Iterators not yet, mainly because the built in collection classes already do what I need. Nullable types? I want to since they match up with nullable fields in databases so nicely, but the effort involved in retrofitting what I currently have just makes me cringe. It'll happen next clean-up iteration, but not today. And anonymous methods? To me they are like the DataSource control in ASP.NET. They let you get something done quickly and without needing to bother anyone else, but they just scream out "quick and dirty" to me. Give the code the respect of at least having a name and a place to live.
cheers, Chris Maunder
CodeProject.com : C++ MVP
Chris Maunder wrote:
Nullable types? I want to since they match up with nullable fields in databases so nicely, but the effort involved in retrofitting what I currently have just makes me cringe. It'll happen next clean-up iteration, but not today.
Yeah, I use them a fair bit, for situations where my own code holds a number, but would otherwise need a 'magic' non-valid value like -1.
Chris Maunder wrote:
They let you get something done quickly and without needing to bother anyone else, but they just scream out "quick and dirty" to me. Give the code the respect of at least having a name and a place to live.
*grin* That's exactly how I've always felt, but like I said, this one time I realised they fit my need, because the code in the method where they are created has scope for the method, and the timer needed to find, remove and destroy a control that the calling method had created. So, it made my code a fair bit simpler. I've always thought iterators where the best of the new features, but, like you, I've not needed them yet, excepting for one time I needed a method to randomise a collection. I'm surprised that's not in there ( or is it ? )
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Generics, anonymous methods, yield, DynamicMethod, static and partial classes, separate visibility levels for property accessors, delegate co/contravariance, Marshal.GetDelegateForFunctionPointer(), debugger visualizers - I've used all of those and more. .NET 2.0 is :cool: !
Debug visualisers where the first new feature I used, and writing some intense image processing code, they saved my bacon more than once. But, that's an IDE feature, not a language feature, so I didn't include it.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
J. Dunlap wrote:
delegate co/contravariance
What's that?
J. Dunlap wrote:
Marshal.GetDelegateForFunctionPointer(),
When do you use that?
J. Dunlap wrote:
debugger visualizers
Are these things you've written, or using the visualizers already in the debugger?
J. Dunlap wrote:
separate visibility levels for property accessors
Ah, I forgot about that!
J. Dunlap wrote:
DynamicMethod
Say, do you have those dynamic property setters folded into MyXaml yet? ;P Marc
Some people believe what the bible says. Literally. At least [with Wikipedia] you have the chance to correct the wiki -- Jörgen Sigvardsson
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmerMarc Clifton wrote:
Are these things you've written, or using the visualizers already in the debugger?
Hah !!! The ones provided are useless. Why no bitmap visualiser ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
I'm interested ( in light of the post below ) to know how many people have taken to the new 2.0 features ? We all use typed containers, I am sure, but how often do you use an anonymous method or iterators ? As I said on that thread, I used my first anonymous method, because it struck me as the ideal solution as I worked on it, before it's never seemed appropriate. I wonder if I'm just not in the habit yet, or this was truly the first time they were the right tool for the job. I love the idea of iterators, but I don't think I've used them in production code yet, and I imagine I need to look out for chances to change my mindset to be using them. How about you ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
CG, I have a huge library of classes that work on generic IEnumerable<T> and ICollection<T>; the whole library depends heavily on anonymous methods, in particular, Action<T> or Predicate<T> anonymous methods, and heavily relies on yield return iterators. Those things are so dang cool; the delayed execution opens up some interesting possibilities that didn't previously exist. I use the things for flattening hierarchies (yield returning the whole hierarchy recursively), returning an unknown set of results (say, any number matching some criteria specified by a Predicate<T> ), lots of cool stuff. :cool: So for me, of course generics is the great enabler and tops my usage list. Shortly after comes anonymous methods, then yield returns. All excellent features very useful in their current state. C# 3 builds heavily on these by introducing things like LINQ, lambda expressions, both of which use anonymous methods and yield returns under the hood.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Last modified: Monday, August 14, 2006 9:39:31 PM --
-
Still copy'n'paste in my book. :) List == IntList, List == StringList. Semantically, wherever you can put a List, you can put an IntList. The obvious benefit with the generics is that you don't have tio write all the methods (Append(), Delete(), etc) for each and every type you want to use. Ok, so I'll meet you halfway; it's copy'n'link for you, but copy'n'paste for the compiler/linker/JIT (it still has to "copy" for each new instantiation of the generic). :)
-- [LIVE] From Omicron Persei 8
Jörgen Sigvardsson wrote:
it still has to "copy" for each new instantiation of the generic
Actually that's not true for .NET generics. For performance reasons, it will create a new instantiation for each value type (IntList, DoubleList, BoolList, etc) but for reference types (StringList, ObjectList, MyFooList) it uses only a single in-memory instantiation that works on reference types. :cool:
Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
My hopes in .Net 2.0 was Generics BUT... I wished create some type like MyBox Now I wish a collection of MyBox objects, but I can't. I can have only a collection of MyBox, MyBox, etc, but not of "MyBox" :doh: Another problem is the ?? operator and the Nullable notation (int?, string?, etc). How I can have problems using these together, I ever use Nullable instead... I will use iterators when I have nothing more important to do... The only thing what I use and abuse is the provider model (membership, sitemap, etc). But have the problem what is developed to read configurations of the web.config. If you wish use in Windows Forms, will be needed a lot of work to read from app.config (I have mentioned, what even if you use class libraries, you need referencing things like System.Web.Configuration, Syste.Web.Security, etc) This is my points and rants :) -- modified at 19:39 Monday 14th August, 2006 Jesus is Love! Tell to someone! :-)
click.ok wrote:
Now I wish a collection of MyBox objects, but I can't.
I agree with Jorgen, this really isn't an issue if you use generics/templates correctly IMO.
click.ok wrote:
Another problem is the ?? operator and the Nullable notation (int?, string?, etc). How I can have problems using these together, I ever use Nullable instead...
I'm not sure what you're talking about. int? and double? is the same as Nullable<int> and Nullable<double>. You can use the ?? syntax on any nullable type, including Nullable and reference types.
click.ok wrote:
I will use iterators when I have nothing more important to do...
Man, you're missing out my friend. :) yield return rocks, and allows one to achieve things previously very difficult to do. If you haven't checked them out, I'd love to show you some cool possibilities yield return opens up for you.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
I'm interested ( in light of the post below ) to know how many people have taken to the new 2.0 features ? We all use typed containers, I am sure, but how often do you use an anonymous method or iterators ? As I said on that thread, I used my first anonymous method, because it struck me as the ideal solution as I worked on it, before it's never seemed appropriate. I wonder if I'm just not in the habit yet, or this was truly the first time they were the right tool for the job. I love the idea of iterators, but I don't think I've used them in production code yet, and I imagine I need to look out for chances to change my mindset to be using them. How about you ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
I think I use them all regularly. At least I'm using lots of: - generic (I had an unpleasant surprise a while ago: MyClass and MyClass have nothing in common! other than that they are cool). - anonymous delegate are great! (in fact I get used to the like of them in Java 2.0) - iterator. real cool once I understood I was not limited to GetEnumerator() but can be used to define all sorts of methods - myEvent += myMethod or myEvent += delegate(){..} (omitting the new EventHandler()) is great! - I use Nullable type quite less often, but they do come handy sometimes. And, most of all, I love the new Managed C++ 2.0, much less ambiguous and verbose than C++ cli 1.0 That's it... did I miss something? :~
-
Marc Clifton wrote:
As to iterators, I have yet to write my own iterator or type the word "yield". That's probably because of the nature of the code I write, rather than whatever kind of code you write that would leverage iterators.
I haven't written a line of code using the yield stuff. But from what I can tell, it does have potential for clever patterns. Do you know if yield can handle non-linear collections such as dictionaries?
-- Made From Meat By-Products
Jörgen Sigvardsson wrote:
Do you know if yield can handle non-linear collections such as dictionaries?
Yeah, you can iterate over any IEnumerable, including Dictionary<T>, and yield return anything you want from them. Iterators are actually very cool; they're somewhat a form of continuations the functional programming world knows well. Basically they let you return pieces of information on-demand rather than upfront: for example, you could yield return all even numbers. The beauty is that the code itself doesn't get executed until actually needed; so instead of waiting for a function to create a huge array containing all even numbers, consumers simply start iterating over your function's result and immediately get a result back. Each iteration will go to the next yield return; thus allowing all sorts of amazing possibilities previously unique to functional programming. IMO, iterators are a great feature in the language. Very cool stuff. :cool:
Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Chris Maunder wrote:
Nullable types
Nullable types are utterly useless, IMHO, because they have no correspondence to DBNull.Value, which is what I care about. Marc
Some people believe what the bible says. Literally. At least [with Wikipedia] you have the chance to correct the wiki -- Jörgen Sigvardsson
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmerMarc Clifton wrote:
Nullable types are utterly useless
I've found some interesting uses for them. One recent thing I've been throwing around is lazy initialization of a value type. Say you've some integer that is only retrieved once, but that one-time retrieval requires a lot of work. So you get the value and store it later, right? Without nullables, you might do something like:
bool hasRetrievedFoo;
int foo;...
public int Foo
{
get
{
if(!hasRetrievedFoo)
{
this.foo = DoSomeWorkToGetFoo();
this.hasRetrievedFoo = true;
}
return this.foo;
}
}It's not much, but with nullable we can eliminate the need for the boolean field.
int? foo;
...
public int Foo
{
get
{
if(foo == null)
{
foo = DoSomeWorkToGetFoo();
}
return foo.Value;
}
}The code is now more concise with fewer lines to accomplish the same task. Another thing I've found them useful for is in my data layer, calling stored procedures that take nullable parameters or returns nullable fields. Unfortunately you have to map DBNull to Nullable<T>, but that's better than the hack we used to have to do without nullables.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
click.ok wrote:
Now I wish a collection of MyBox objects, but I can't.
I agree with Jorgen, this really isn't an issue if you use generics/templates correctly IMO.
click.ok wrote:
Another problem is the ?? operator and the Nullable notation (int?, string?, etc). How I can have problems using these together, I ever use Nullable instead...
I'm not sure what you're talking about. int? and double? is the same as Nullable<int> and Nullable<double>. You can use the ?? syntax on any nullable type, including Nullable and reference types.
click.ok wrote:
I will use iterators when I have nothing more important to do...
Man, you're missing out my friend. :) yield return rocks, and allows one to achieve things previously very difficult to do. If you haven't checked them out, I'd love to show you some cool possibilities yield return opens up for you.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Judah Himango wrote:
yield return rocks, and allows one to achieve things previously very difficult to do. If you haven't checked them out, I'd love to show you some cool possibilities yield return opens up for you.
Excuse my ignorance, but what does "yield return" mean and why does it rock?
- S 50 cups of coffee and you know it's on!
-
Jörgen Sigvardsson wrote:
Do you know if yield can handle non-linear collections such as dictionaries?
Yeah, you can iterate over any IEnumerable, including Dictionary<T>, and yield return anything you want from them. Iterators are actually very cool; they're somewhat a form of continuations the functional programming world knows well. Basically they let you return pieces of information on-demand rather than upfront: for example, you could yield return all even numbers. The beauty is that the code itself doesn't get executed until actually needed; so instead of waiting for a function to create a huge array containing all even numbers, consumers simply start iterating over your function's result and immediately get a result back. Each iteration will go to the next yield return; thus allowing all sorts of amazing possibilities previously unique to functional programming. IMO, iterators are a great feature in the language. Very cool stuff. :cool:
Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Judah Himango wrote:
Basically they let you return pieces of information on-demand rather than upfront: for example, you could yield return all even numbers. The beauty is that the code itself doesn't get executed until actually needed; so instead of waiting for a function to create a huge array containing all even numbers, consumers simply start iterating over your function's result and immediately get a result back. Each iteration will go to the next yield return; thus allowing all sorts of amazing possibilities previously unique to functional programming.
Ah, I get it now, never mind - I should've read this whole post, before posting above. That does sound pretty cool. I can think of some date/time recurrency functions where this could be useful.
- S 50 cups of coffee and you know it's on!