MSDOTnet.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

operator new with "value type"
Goto page 1, 2  Next
 
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp
Author Message
carnold



Joined: 07 Dec 2007
Posts: 2

PostPosted: Fri Dec 07, 2007 5:22 pm    Post subject: operator new with "value type" Reply with quote

Hello,

I'm a developer coming from C++ and Java ... I've going thru
"Effective C#" (which highly recommend for people coming from other
languages wanting to learn C#), and it states that "value types" are
always created on the stack, but then showed the "creation" of one w/
operator new. I guess it makes sense, but is it true that:

MyValueType t;

and

MyValueType t = new MyValueType();

are equivalent w/ regard to where the object is stored? That just
seems odd to me. I suppose this is probably in a FAQ somewhere (I
welcome any pointers to any FAQ list that answers this), but I
couldn't find it stated obviously (tho I didn't spend too long
looking Smile ...

-Charlie

Archived from group: microsoft>public>dotnet>languages>csharp
Back to top
View user's profile Send private message
Marc Gravell



Joined: 08 Aug 2007
Posts: 20

PostPosted: Sat Dec 08, 2007 5:23 am    Post subject: Re: operator new with "value type" Reply with quote

Just as a minor comment on the "new" thing... the use of the same
keyword certainly help keep things consistent when considering
generics... if there were two operators, then it would be a little
less clear what "new T()" meant for "SomeMethod() where T : new()".
(I'm mainly thinking of default values, and "not found" return values
in the "bool TrySomething(out T)" pattern).

Yes, value-types have different behavior - but that isn't really
limited just to construction. They behave differently as method
arguments, for instance - yet the syntax is identical there... I'm not
sure it makes much benefit to single out construction for special
syntax. Personally I'm happy with the shared syntax, but I appreciate
that there is a little bit of a learning curve.

Marc
Back to top
View user's profile Send private message
Peter Duniho



Joined: 08 Aug 2007
Posts: 45

PostPosted: Sat Dec 08, 2007 7:08 am    Post subject: Re: operator new with "value type" Reply with quote

On Sat, 08 Dec 2007 00:23:15 -0800, Marc Gravell
wrote:

> Just as a minor comment on the "new" thing... the use of the same
> keyword certainly help keep things consistent when considering
> generics... if there were two operators, then it would be a little
> less clear what "new T()" meant for "SomeMethod() where T : new()".
> (I'm mainly thinking of default values, and "not found" return values
> in the "bool TrySomething(out T)" pattern).

Yup, that's true. Anyone arguing against using "new" to initialize a
struct should be forced to come up with a suitable alternative syntax so
that a generic class or method that has to initialize/instantiate some
instance of one or more type parameters for the generic can still be done
without caring whether the type is a value or reference type.

So, do I have an alternative syntax? No, not at the moment. I'll get
back to you. Smile

> Yes, value-types have different behavior - but that isn't really
> limited just to construction. They behave differently as method
> arguments, for instance - yet the syntax is identical there... I'm not
> sure it makes much benefit to single out construction for special
> syntax. Personally I'm happy with the shared syntax, but I appreciate
> that there is a little bit of a learning curve.

It doesn't bother me that much now. In fact, it never really created a
significant problem for me, per se. The main problem was that it for a
little while led me to believe a few types were classes instead of
structs. I also have a vague memory of some sort of uninitialized
variable error that was somehow related to this "new" business, but at the
moment I can't actually remember why it was related (whatever it was, it
was more complicated than just "oh, you need new to initialize your value
type" but I don't recall the details).

But it didn't cause me any hard-to-figure out coding problems and of
course I eventually got better at thinking value-vs-reference type without
getting confused by the overlapping syntax.

I can't say I would ever lobby hard to change the syntax. But I do have
sympathy for those who feel compelled to complain about it. Smile And
believe me, if I had a good idea for how to deal with that situation
better, I'd talk about it. I do see the benefit in the current syntax,
and I have no suggestions for something better. But that doesn't mean I
have to like it. Smile

Pete
Back to top
View user's profile Send private message
Marc Gravell



Joined: 08 Aug 2007
Posts: 20

PostPosted: Sun Dec 09, 2007 7:00 am    Post subject: Re: operator new with "value type" Reply with quote

> The main problem was that it for a
> little while led me to believe a few types were classes instead of
> structs.

I agree that this can be a pain, and it is *so* important to know if
you are talking about a class or a struct - I'm lazy, so I simply
change the IDE color of the various "User Types (...)" items; my
"Value Types" are in magenta, for example. Very helpful when you are
working with something unfamiliar.

(it obviously doesn't help with keyword aliases (int vs string), but
I'm pretty sure I know all of those ;-p)

Marc
Back to top
View user's profile Send private message
Ben Voigt [C++ MVP]



Joined: 08 Aug 2007
Posts: 189

PostPosted: Tue Dec 11, 2007 3:09 pm    Post subject: Re: operator new with "value type" Reply with quote

> Another change from C++ and Java: you can't fall through from one
> switch case to another (although you can have multiple labels for the
> same case). Is that a bad idea too? It removes a common source of
> errors.

Ahh, but you *can* fall-through when you need to, using "goto case". The
best of both worlds -- protection against stupid errors, and the ability to
do what you need where you need it. Even a rather intuitive syntax for
letting the compiler know that the out-of-the-ordinary was intended.
Back to top
View user's profile Send private message
Jon Skeet [C# MVP]



Joined: 08 Aug 2007
Posts: 27

PostPosted: Tue Dec 11, 2007 1:41 pm    Post subject: Re: operator new with "value type" Reply with quote

On Dec 11, 4:09 pm, "Ben Voigt [C++ MVP]" wrote:
> > Another change from C++ and Java: you can't fall through from one
> > switch case to another (although you can have multiple labels for the
> > same case). Is that a bad idea too? It removes a common source of
> > errors.
>
> Ahh, but you *can* fall-through when you need to, using "goto case". The
> best of both worlds -- protection against stupid errors, and the ability to
> do what you need where you need it. Even a rather intuitive syntax for
> letting the compiler know that the out-of-the-ordinary was intended.

True - but I wouldn't personally call "explicitly saying where to go
next" as fall-through Smile

(One slight pity about the C# 3 spec is that in one place where it
shows how C# has removed a common source of C++ errors, it uses an if
without braces - demonstrating how a completely different source of C+
+ errors is still alive and kicking in C#!)

Jon
Back to top
View user's profile Send private message
Ignacio Machin \( .NET/ C



Joined: 08 Aug 2007
Posts: 16

PostPosted: Tue Dec 11, 2007 6:00 pm    Post subject: Re: operator new with "value type" Reply with quote

Hi,


--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Jon Skeet [C# MVP]" wrote in message
> (One slight pity about the C# 3 spec is that in one place where it
> shows how C# has removed a common source of C++ errors, it uses an if
> without braces - demonstrating how a completely different source of C+
> + errors is still alive and kicking in C#!)

I have never seem that error, have you?
Back to top
View user's profile Send private message
Jon Skeet [C# MVP]



Joined: 08 Aug 2007
Posts: 266

PostPosted: Tue Dec 11, 2007 11:06 pm    Post subject: Re: operator new with "value type" Reply with quote

>
wrote:
>> (One slight pity about the C# 3 spec is that in one place where it
>> shows how C# has removed a common source of C++ errors, it uses an
>> if without braces - demonstrating how a completely different source
>> of C++ errors is still alive and kicking in C#!)

> I have never seem that error, have you?

Yes, I have. Basically code which ended up reading:

if (someExpression)
FirstStatement();
SecondStatement();

but should have read:

if (someExpression)
{
FirstStatement();
SecondStatement();
}

Not my code, of course, but I a bug I fixed Smile

--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Back to top
View user's profile Send private message
Ben Voigt [C++ MVP]



Joined: 08 Aug 2007
Posts: 189

PostPosted: Thu Dec 13, 2007 2:40 pm    Post subject: Re: operator new with "value type" Reply with quote

"Jon Skeet [C# MVP]" wrote in message @msnews.microsoft.com...
> >
> wrote:
>>> (One slight pity about the C# 3 spec is that in one place where it
>>> shows how C# has removed a common source of C++ errors, it uses an
>>> if without braces - demonstrating how a completely different source
>>> of C++ errors is still alive and kicking in C#!)
>
>> I have never seem that error, have you?
>
> Yes, I have. Basically code which ended up reading:
>
> if (someExpression)
> FirstStatement();
> SecondStatement();
>
> but should have read:
>
> if (someExpression)
> {
> FirstStatement();
> SecondStatement();
> }
>
> Not my code, of course, but I a bug I fixed Smile

python programmers...

>
> --
> Jon Skeet -
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk
Back to top
View user's profile Send private message
Arne_Vajhøj



Joined: 08 Aug 2007
Posts: 39

PostPosted: Mon Dec 24, 2007 1:19 am    Post subject: Re: operator new with "value type" Reply with quote

Jon Skeet [C# MVP] wrote:
> >
> wrote:
>>> (One slight pity about the C# 3 spec is that in one place where it
>>> shows how C# has removed a common source of C++ errors, it uses an
>>> if without braces - demonstrating how a completely different source
>>> of C++ errors is still alive and kicking in C#!)
>
>> I have never seem that error, have you?
>
> Yes, I have. Basically code which ended up reading:
>
> if (someExpression)
> FirstStatement();
> SecondStatement();
>
> but should have read:
>
> if (someExpression)
> {
> FirstStatement();
> SecondStatement();
> }
>
> Not my code, of course, but I a bug I fixed Smile

It must have happen hundred of thousands of times.

The programmer writes:

if (someExpression)
SecondStatement();

He figure out that he does not need the { } because it is only
one statement.

Then later another statement has to be added and because it is a bit
hectic at the time then it end up as:

if (someExpression)
FirstStatement();
SecondStatement();

And suddenly some things stop working.

Arne
Back to top
View user's profile Send private message
Arne Vajhøj



Joined: 08 Aug 2007
Posts: 6

PostPosted: Mon Dec 24, 2007 1:21 am    Post subject: Re: operator new with "value type" Reply with quote

Lew wrote:
> In fact, within FORTRAN itself there were changes within "familiar
> constructs". A loop index variable was scoped to the loop in some
> dialects, but wider in others. The newer languages changed the meaning
> of the familiar construct of "scope", and made it explicit, thus fixing
> a lot of weird bugs.

Fortran 90/95 ?

Not possible in 66 and 77.


(I know that C++ had such a problem before ANSI standardization)

Arne
Back to top
View user's profile Send private message
Ignacio Machin \( .NET/ C



Joined: 08 Aug 2007
Posts: 16

PostPosted: Mon Dec 24, 2007 4:44 am    Post subject: Re: operator new with "value type" Reply with quote

Hi,


Interesting, it might be the case that I have not seen that much code that
was not mine Smile

It would be overkill to force the use of { though.

"Arne Vajhøj" wrote in message $0$90264$14726298@news.sunsite.dk...
> Jon Skeet [C# MVP] wrote:
>> >
>> wrote:
>>>> (One slight pity about the C# 3 spec is that in one place where it
>>>> shows how C# has removed a common source of C++ errors, it uses an if
>>>> without braces - demonstrating how a completely different source of C++
>>>> errors is still alive and kicking in C#!)
>>
>>> I have never seem that error, have you?
>>
>> Yes, I have. Basically code which ended up reading:
>>
>> if (someExpression)
>> FirstStatement();
>> SecondStatement();
>>
>> but should have read:
>>
>> if (someExpression)
>> {
>> FirstStatement();
>> SecondStatement();
>> }
>>
>> Not my code, of course, but I a bug I fixed Smile
>
> It must have happen hundred of thousands of times.
>
> The programmer writes:
>
> if (someExpression)
> SecondStatement();
>
> He figure out that he does not need the { } because it is only
> one statement.
>
> Then later another statement has to be added and because it is a bit
> hectic at the time then it end up as:
>
> if (someExpression)
> FirstStatement();
> SecondStatement();
>
> And suddenly some things stop working.
>
> Arne
Back to top
View user's profile Send private message
Jon Skeet [C# MVP]



Joined: 08 Aug 2007
Posts: 266

PostPosted: Mon Dec 24, 2007 1:14 pm    Post subject: Re: operator new with "value type" Reply with quote

>
wrote:
> Interesting, it might be the case that I have not seen that much code that
> was not mine Smile
>
> It would be overkill to force the use of { though.

Would it really? I certainly find code much more readable that way, and
it *is* required for various constructs IIRC.

I use them everywhere, and haven't found any disadvantages to it.

--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Back to top
View user's profile Send private message
Ben Voigt [C++ MVP]



Joined: 08 Aug 2007
Posts: 189

PostPosted: Mon Dec 24, 2007 3:04 pm    Post subject: Re: operator new with "value type" Reply with quote

"Jon Skeet [C# MVP]" wrote in message @msnews.microsoft.com...
> >
> wrote:
>> Interesting, it might be the case that I have not seen that much code
>> that
>> was not mine Smile
>>
>> It would be overkill to force the use of { though.
>
> Would it really? I certainly find code much more readable that way, and
> it *is* required for various constructs IIRC.

do-while and switch require the braces

>
> I use them everywhere, and haven't found any disadvantages to it.
>
> --
> Jon Skeet -
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk
Back to top
View user's profile Send private message
Barry Kelly



Joined: 08 Aug 2007
Posts: 42

PostPosted: Tue Dec 25, 2007 5:06 am    Post subject: Re: operator new with "value type" Reply with quote

Ben Voigt [C++ MVP] wrote:

> "Jon Skeet [C# MVP]" wrote in message
> @msnews.microsoft.com...
> > >
> > wrote:
> >> Interesting, it might be the case that I have not seen that much code
> >> that
> >> was not mine Smile
> >>
> >> It would be overkill to force the use of { though.
> >
> > Would it really? I certainly find code much more readable that way, and
> > it *is* required for various constructs IIRC.
>
> do-while and switch require the braces

---8<---
class App
{
static void Main()
{
do System.Console.WriteLine(); while(false);
}
}
--->8---

C# switch does require braces, although consider C/C++:

---8<---
int main(void)
{
switch (0);
return 0;
}
--->8---

(I in no way condone use of the above forms; though I'm ambivalent about
requiring {} on if-statement sub-blocks.)

-- Barry

--
http://barrkel.blogspot.com/

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
new operator with deep class structure Hi, I generated C# classes from some complex XMLSchemas usind xsd.exe. The result is that I get a class hierarchy that is quite deep (well for me 8 levels are deep). What I'm curiuos about is, that if I create an instance of my top level element I still n

Help: The type library has lots of unexpected struct type de Hi, We have a COM+ component developed in managed C++. After using regsvcs.exe to install the component, we checked the type library. For ..Net 2002, only the interface and COM object defined in the component are listed in tlb, but .Net 2003 gives a lot o

how to cast IntPtr type to struct type Hi all, how to cast an System.IntPtr value to struct type. Example: protected override void WndProc(ref m) { const int = 0x0047; if(m.Msg == { string str=""; str);

Data Type In VB6 I use this code: Public Island() as MapPoint Type MapPoint GridEast as Single GridNorth as Single End Type How do I do this in VB.NET?

app.exe.config - how do I add N keys of the same type? Hi; I need to do something like this:
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group