c# features

J

Jarlaxle

I have to say I think that lambda functions are incredible. Although a
little disconcerting at first I think they really unclutter code and are
extremely powerful when you understand them.

So here is my wish list for c#/cli features...

1. static extension methods - can't see a reason for not having them
2. return by reference! - you can pass in by ref why not return by ref
3. automatic properties that create...

how many times do you write the following for lazy creation...

private mytype myvar;

public mytype myvar
{
get
{
if (myvar == null) myvar = new mytype();
return myvar;
}
}

would be cool to have...

public mytype myvar
{
autoget; //or whatever name for parameterless constructor
set;
}

4. DateTime that stores in utc (DateTimeUTC)
- the problem with the DateTimeOffset class is it uses two fields and
therefore no xml serialization support (unless im missing something)
 
P

Peter Duniho

I have to say I think that lambda functions are incredible. Although a
little disconcerting at first I think they really unclutter code and are
extremely powerful when you understand them.

So here is my wish list for c#/cli features...

1. static extension methods - can't see a reason for not having them

I can't see a reason _for_ having them. A static method requires that you
specify a type name for the method already, so why is better to specify a
type name that completely misleads the reader of the code as to where that
method is actually declared?

Extension methods as they are happen to be potentially misleading as well,
but at least there's a genuine balancing benefit. I don't see what the
benefit for a static extension method would be.
2. return by reference! - you can pass in by ref why not return by ref

What point would there be in returning "by ref"? To what would the
reference refer? So far the only thing I've seen you suggest as a benefit
is performance, and I don't believe that you'd gain any appreciable
performance by allowing returning value types by reference.
3. automatic properties that create...

Unlike automatic properties introduces in C#, what you're suggesting has
very limited utility and IMHO doesn't belong as a language feature.

I don't want a kitchen sink language. I want a language that does
relatively few things, but does those things very well. Granted,
especially with C# 3.0, they seem to be moving toward kitchen-sink-ness,
and who knows? Maybe you'll get your wish eventually. But I don't see
that as being something the language should be implementing itself.
4. DateTime that stores in utc (DateTimeUTC)
- the problem with the DateTimeOffset class is it uses two fields
and
therefore no xml serialization support (unless im missing something)

First of all, DateTime is a .NET feature, not a C# feature.

But secondly, DateTime can be local or UTC. You just have to set it as
desired.

Pete
 
P

Peter Ritchie [C# MVP]

I have to say I think that lambda functions are incredible. Although a
little disconcerting at first I think they really unclutter code and are
extremely powerful when you understand them.

So here is my wish list for c#/cli features...

1. static extension methods - can't see a reason for not having them

"can't see a reason for not having them" isn't a reason for having them.
Give the extension method syntax (returnType MethodName(this typeName
instanceObject[...optional arguments]) what would you expect the instance
object to contain? null? 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. This likely
means a new syntax is required; at which point is much more complex than
simply "can't see a reason for not having them". Was there a particular use
case you think would make the feature useful?
2. return by reference! - you can pass in by ref why not return by ref
Return by reference is free when using references. If you're talking about
return by ref for value types, you're getting into the area of pointers, not
something that belongs in a managed langue. If that's what you want, use
pointers.
4. DateTime that stores in utc (DateTimeUTC)
- the problem with the DateTimeOffset class is it uses two fields and
therefore no xml serialization support (unless im missing something)
Lack of support for XML serialization shouldn't affect how DateTime stored
data. Besides, that's an implementation detail; as long as you can get a UTC
representation of a date that's all that matters. I would suggest requesting
XML serialization support for DateTimeOffset.
 
J

Jarlaxle

hey pete...inline...

Peter Duniho said:
I can't see a reason _for_ having them. A static method requires that you
specify a type name for the method already, so why is better to specify a
type name that completely misleads the reader of the code as to where that
method is actually declared?

Extension methods as they are happen to be potentially misleading as well,
but at least there's a genuine balancing benefit. I don't see what the
benefit for a static extension method would be.

the benefit would be exactly the same benefit of an instance extension.
extension methods alleviate the million helper classes that non-trivial apps
tend to have.

i think saying you can't see a reason for them and therefore shouldn't have
them is ridiculous. why not let the individual developer decide if there is
not a valid reason to omit them? why polute the namespace with a bunch of
other types when they should be grouped accordingly.
What point would there be in returning "by ref"? To what would the
reference refer? So far the only thing I've seen you suggest as a benefit
is performance, and I don't believe that you'd gain any appreciable
performance by allowing returning value types by reference.

to not make a copy of the object is the point of returning by ref. it would
refer to the object. there are really two benefits to being able to return
by reference and that is exposing a value type by property. that is enough
in itself but a more important reason is performance. there's no appreciable
gain by returning a 4/8 byte pointer rather than a 200 byte object???

Unlike automatic properties introduces in C#, what you're suggesting has
very limited utility and IMHO doesn't belong as a language feature.

I don't want a kitchen sink language. I want a language that does
relatively few things, but does those things very well. Granted,
especially with C# 3.0, they seem to be moving toward kitchen-sink-ness,
and who knows? Maybe you'll get your wish eventually. But I don't see
that as being something the language should be implementing itself.

well theres a reason why automatic properties were introduced and that is to
reduce boilerplate code. however they really are only useful on primitive
types and tightly controlled scenarios. when you find yourself writing the
same code a hundred zillion times (as im sure others have also) then its time
for a built-in feature (or a way to create one yourself). anyone miss macros?

First of all, DateTime is a .NET feature, not a C# feature.

But secondly, DateTime can be local or UTC. You just have to set it as
desired.

Pete

Yes I believe I said c#/cli features just for this case! The problem, as
i'm sure others have run into many times, is that when you write an app that
needs to be interpreted in multiple timezones, you need to always store a utc
value and transmit in utc then interpret for display in the current timezone.

when you have alot of develops working on the same app with a requirement
not to use DateTime.Now or any of the other local tz methods, it is a recipe
for defects and simply is not plausible.

That is why the DateTimeOffset was introduced which 3.0 however it is a two
field structure. I think if it was to be done over again they would have
designed it as utc internally but probably because of compatibility issues it
is a proprietary formula.


I have to say I think c# (along with .net) is an incredibly productive
language. And I think people favor it because of its ease of use and
elimination of tedious tasks. especially when you have been writing c/c++
apps for years.
 
A

Alun Harford

Jarlaxle said:
I have to say I think that lambda functions are incredible. Although a
little disconcerting at first I think they really unclutter code and are
extremely powerful when you understand them.

So here is my wish list for c#/cli features...

1. static extension methods - can't see a reason for not having them

Hmm... Actually I quite like this idea (it would be nice to be able to
'add' static methods to code I don't control). Coming up with a good
syntax is the tricky bit...
2. return by reference! - you can pass in by ref why not return by ref

You already can (at least in MS C#). That's what System.TypedReference,
__makeref and __refvalue are for. Unfortunately, every time you use the
__makeref keyword, an OO purist tortures an innocent child, so I don't
recommend it.
3. automatic properties that create...
Eugh.

4. DateTime that stores in utc (DateTimeUTC)

Already too many DateTime classes.

Alun Harford
 
P

Peter Duniho

the benefit would be exactly the same benefit of an instance extension.

That's a trivially false statement. Given that a primary benefit of
instance extensions is to be able to implicitly call a static method that
takes an instance of a particular type on an instance of an object of that
type, obviously a "static extension" would not be "exactly the same".
extension methods alleviate the million helper classes that non-trivial
apps
tend to have.

Huh? You don't get rid of the helper classes. They're still there. You
just don't have to type the name of them explicitly when you use them.
And since a static extension would still require you to type the name of
_some_ class, why not just type the name of the class where the method is
really defined?
i think saying you can't see a reason for them and therefore shouldn't
have
them is ridiculous.

Well, when you provide an actual example of how a static extension would
be useful, I might change my mind. But I think it's ridiculous for you to
insist that I agree with you before you've proven your point.

I don't mind having my mind changed by a strong argument, but you have to
present that argument first.
why not let the individual developer decide if there is
not a valid reason to omit them? why polute the namespace with a bunch
of
other types when they should be grouped accordingly.

The namespace is already "polluted", even with instance extensions. You
just don't have to type the class name. But it's still there. So that's
not a valid argument in favor of a static extension.

What's left? Making the static method look like it belongs to some other
class (the one you're extending). What's the point of making a static
method look like it belongs to some other class? Other than the name you
type, it will behave _exactly_ as it would if you just used it from your
own class.

With an instance extension, you can really create code that makes it look
like you've added a method to an instance of a given object, and that can
clean up the code a lot. But with a static extension, all you can do is
swap one name for another. How is that useful? Why does it matter to you
so much which exact letters you type when calling a method?

As far as "return by reference" goes...
to not make a copy of the object is the point of returning by ref.

But that's impossible to do with a value type. C# has no syntax for
referring to value types as if they were referenced. Where would that "no
copy of the object" come from? The run-time would essentially have to box
the object, and then provide some sort of new semantic that allows the use
of the object without unboxing it.

But C# already has a semantic for using an object without unboxing it.
It's called reference types. And if you need to use a type in that way,
then a reference type is what you should be using.

In any case, even if you somehow managed to get a "return by reference"
into the language, you'd have to make sweeping changes to the rest of the
language (and likely the run-time) in order to support that change. As
things stand now, if it's a value type, you can't use it without making a
copy of it. This would be true even if you somehow managed to return a
reference to a copy of it.
it would
refer to the object. there are really two benefits to being able to
return
by reference and that is exposing a value type by property.

What does that mean? What does "exposing a value type by property" mean?
I know what the individual words mean, but put together like that they
don't convey any useful information to me.
that is enough
in itself but a more important reason is performance. there's no
appreciable
gain by returning a 4/8 byte pointer rather than a 200 byte object???

If you have an object that's (for example) 200 bytes, it doesn't really
belong in a struct in the first place. You're trying to fix broken code
by breaking something else, rather than just fixing the broken code.

Then, we have your "lazily instantiated" properties...
well theres a reason why automatic properties were introduced and that
is to
reduce boilerplate code.

Let's be clear: it reduces boilerplate code >> that is used on a
remarkably common basis << .
however they really are only useful on primitive
types and tightly controlled scenarios.

How so? They are useful in _any_ situation in which you have a property
that is simply a getter and/or setter for some field. That works with any
type, not just primitives, and it hardly requires that the scenario is
"tightly controlled".
when you find yourself writing the
same code a hundred zillion times (as im sure others have also) then its
time
for a built-in feature (or a way to create one yourself).

If you are writing this sort of "lazily instaniated" property "a hundred
zillion times", you are in a completely unique situation and not one that
justifies a whole new language feature just to support your own personal
problem.

Suffice to say, most programmers write entire applications without ever
having to lazily initialize a field behind a property, and even those that
do only have to do for a handful of properties at most. And even _then_,
in many of those situations a parameterless constructor isn't sufficient
(and may not even be available).

If this sort of thing applies in the vast majority of your code, you have
very unusual code. You might want to look into the "code snippet" feature
of Visual Studio, but otherwise your own needs do not in any way suggest a
need for a new language feature. In the broader scheme of things, code
that does what you're talking about is extremely rare.
anyone miss macros?

Nope, not really.
Yes I believe I said c#/cli features just for this case! The problem, as
i'm sure others have run into many times, is that when you write an app
that
needs to be interpreted in multiple timezones, you need to always store
a utc
value and transmit in utc then interpret for display in the current
timezone.

So? Then do that. Store the data as UTC (which you can do) and then
convert to the local timezone for display (which you can also do). The
latest .NET even provides direct support for converting to timezones other
than the local one (which admittedly was a significant failing in previous
versions).

What feature are you looking for here that doesn't already exist?

Pete
 
J

Jesse McGrew

On Mar 17, 10:59 am, Jarlaxle <[email protected]>
wrote:
[...]
3. automatic properties that create...

how many times do you write the following for lazy creation...

private mytype myvar;

public mytype myvar
{
get
{
if (myvar == null) myvar = new mytype();
return myvar;
}

}

I think I might have written something like that once or twice. Are
you really doing it so often that you want special language support
for it? Why?

Jesse
 
B

Ben Voigt [C++ MVP]

As far as "return by reference" goes...
But that's impossible to do with a value type. C# has no syntax for
referring to value types as if they were referenced. Where would

Yes it does. Pointers.
that "no copy of the object" come from? The run-time would
essentially have to box the object, and then provide some sort of new
semantic that allows the use of the object without unboxing it.

Why would it have to box the object? It could simply use a pointer
internally, just like it does for reference parameters and reference types.

Creating references to stack variables could be forbidden to enhance type
safety (even though for example a reference to a ref parameter is guaranteed
to still be valid in your caller's context, that isn't transitively true).
 
B

Ben Voigt [C++ MVP]

Peter said:
Relevant only for unsafe code and that exists today. We are talking
here about a new feature in C#. It's true that you could implement
something like it using unsafe code in C# now, but if you're going to
raise that as somehow relevant, then the OP's complaint that what he
wants to do is unsupported is wrong from the outset.

Personally, I took it as granted that he's not looking for a solution
that involves unsafe code. Since you didn't offer the use of a
pointer as a return type as a solution to his original question, it
seems to me that you did too. So I am wondering what the point of
your reply to my post here is.

A reference would not be unsafe, because it is a pointer with the unsafe
operations removed.

It cannot be created from a different pointer type or an integer.
It cannot be used in pointer arithmetic.

I'd actually prefer changing the rules for pointers so that only those two
operations need unsafe blocks, and creation/dereference of pointers to value
types on the heap can be used anywhere. But that's much more likely to be a
breaking change rather than a new feature.
It would have to box it because that's the semantics of a value type.
C# currently provides the guarantee that an instance of a value type
cannot be changed except through direct access or passing by
reference as a parameter.

And this would simply extend the case of reference parameters.

The fact that __makeref even exists suggests that it's doesn't conflict with
C# semantics in any way, it just isn't included in the standard.
 
B

Ben Voigt [C++ MVP]

Peter said:
You wrote "pointers". I responded with comments about "pointers". Why
does "a reference would not be unsafe" have any relevance here? We were
talking about pointers, not references.

We were talking about whether the existing feature "pointers" satisfy the
need for returning a reference to a value type instance on the heap. They
do not, because they are rejected outside of unsafe code blocks.
[...]
The fact that __makeref even exists suggests that it's doesn't
conflict with
C# semantics in any way, it just isn't included in the standard.

Huh? I would argue the exact opposite. The fact that __makeref
exists but isn't actually part of the language suggests that it
_does_ conflict with C# semantics and that's why it's not included in
the standard.

The fact that garbage collection does exist, but isn't actually part of the
Win32 API suggests that it does conflict with Win32 semantics!

That's a very nonsensical line of reasoning. Most current parts of C++, for
example, first started out as extensions and later were added to the
standard.
 
B

Ben Voigt [C++ MVP]

Peter said:
Peter said:
On Wed, 19 Mar 2008 12:34:30 -0700, Ben Voigt [C++ MVP]

A reference would not be unsafe, because it is a pointer with the
unsafe operations removed.

You wrote "pointers". I responded with comments about "pointers".
Why does "a reference would not be unsafe" have any relevance here?
We were talking about pointers, not references.

We were talking about whether the existing feature "pointers"
satisfy the need for returning a reference to a value type instance
on the heap. They
do not, because they are rejected outside of unsafe code blocks.

So now you are agreeing with me. Thank you.

I thought you were saying that return by reference isn't needed.

I'm championing the opposite, that pointers aren't good enough so we need
return by reference. You say, make it a reference type. That's really the
problem, .NET forces you to choose between value and reference semantics in
the type declaration, which is a bad architecture. Maybe 99% of the time
value semantics are desired, placing the object inside its container is
desirable for locality of reference, and it doesn't need to be independently
garbage collected. Seems to me that demands a .NET value type. But now for
the 1% of uses which require return by reference, you're going to change the
behavior of every user of that type.
Yes. I don't see anything wrong with that statement. The Win32 API
is a specific API and doesn't take into account garbage collection at
all. In fact, the main reason we have IDisposable and finalizers is
that garbage collection and the Win32 API do not otherwise "get along
with each other".

That's "different", not a conflict.

If there was a conflict, .NET couldn't exist.

And, __makeref IS part of the language. It's not part of the international
standard though.
I realize that for you, C++ may seem like the pinnacle of language
design. Suffice to say, I see C++ differently and just because C++
does something, that doesn't imply to me that it results in an
semantically consistent language. You can call my line of reasoning

There a difference between "inconsistent" and "conflict". "Conflict"
suggests, to me at least, that there can be no coexistance, which certainly
is not the case here.
 

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