c# features - revised!

J

Jarlaxle

alright...one more try at this...at least it got people talking about
it...This is just what I think would be good features...(which of course
doesn't mean jack)...but I think it's good to discuss...

1. static extension methods

I think they would be very useful. for instance adding other methods to the
RegKey class and/or the File class instead of having a ton of helper classes.
It also keeps related methods in one place. it would also allow operator
extensions.

as Peter said the syntax would be an obstacle...
How would you differentiate a "static" extension
method from the instance extension method? You could very well have a null
value for an instance, in which case you couldn't use null.

I'm not sure why you couldn't define it like the following...

returnType MethodName(this typeName null, [...optional arguments])

so for instance...

DateTime Now(this DateTime null, TimeZoneInfo timeZone)
{
//...return currrent time in the given timezone..
}

2. return value by ref

I still think this adds a lot of value...especially in tight loops. it
enables you to have structs as properties instead of direct access which
discourages encapsulation.

As Jon said...
What would happen if you returned a local variable when "returning by
ref"? The variable would have gone by the time the method completed.

This is a very common mistake for beginners in c++. c# could issue a
compiler error..."local value variables can not be returned by reference."
but is useful for static and/or member variables

just my opinion.


* Alright...I could do without auto created properties...I'll just have to
keep declaring the field and the get property body.

3. Here is an idea I thought about last night watching tv (I guess I am a
dork)

(if this is hated by everyone...it does not reflect on the 2 above ideas!!!!)

extension methods that can override and/or block existing methods. before
you start yelling...hear me out...imagine the possibilities!

you could enforce business protocols by using a standard library. for
instance you could have a library that contains all your extension methods.
in that library you could override the RegKey methods to write the values to
a file and then the registry. no code would need to be changed and you could
persist data to a new location.

or you could override the DateTime (or block the local timezone methods) to
always return utc. That would workaround all the DateTime issues.

I know I will get flamed but its a powerful idea. The compiler replaces the
method calls to the overridden extensions.
 
P

Peter Ritchie [C# MVP]

Jarlaxle said:
alright...one more try at this...at least it got people talking about
it...This is just what I think would be good features...(which of course
doesn't mean jack)...but I think it's good to discuss...

1. static extension methods

I think they would be very useful. for instance adding other methods to the
RegKey class and/or the File class instead of having a ton of helper classes.
It also keeps related methods in one place. it would also allow operator
extensions.

Keep your eye on http://blogs.msdn.com/charlie/ and
http://code.msdn.microsoft.com/vslangfutures
2. return value by ref

I still think this adds a lot of value...especially in tight loops. it
enables you to have structs as properties instead of direct access which
discourages encapsulation.

If you want reference semantics instead of value semantics then don't use
structs.
3. Here is an idea I thought about last night watching tv (I guess I am a
dork)

(if this is hated by everyone...it does not reflect on the 2 above ideas!!!!)

extension methods that can override and/or block existing methods. before
you start yelling...hear me out...imagine the possibilities!

No yelling, that would be a breaking change.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top