Re: Roll your own std::vector ???

Tech-Archive recommends: Speed Up your PC by fixing your registry




"Joanna Carter [TeamB]" <joanna@xxxxxxxxxxxx> wrote in message
news:%23xRh1adJHHA.2312@xxxxxxxxxxxxxxxxxxxxxxx
"Peter Olcott" <NoSpam@xxxxxxxxxxxxx> a écrit dans le message de news:
8kRih.128707$xM4.49449@xxxxxxxxxxxxxxx

| > If you call (for instance) myInt.ToString() then boxing isn't involved.

My apologies, I assumed boxing occurred here.

| It would still seem like far less than the best possible design of all
possible
| designs. It still seems like run-time overhead that could have been done
at
| compile-time.

Rathermore, it is usually down to developer laziness, using object
parameters to methods where strictly typed methods would be more efficient
:-)

Many times, developers, wanting to do the same thing to different types,
will use the following method signature :

string ConvertToString(object value)
{
return value.ToString();
}

Now, this means that any type may be passed to the method, but that boxing
will occur. However if, in .NET 1.1, we use overloaded method signatures :

string ConvertToString(int value)
{
return value.ToString();
}


string ConvertToString(double value)
{
return value.ToString();
}

void Test()

{
int i = 123;

string s = ConvertToString(i);
}

... the correct overloaded method will be called for the true type of the
parameter and the native version of ToString() for each type will be called.

Or you could use generics to simplify this even further :


string ConvertToString<T>(T value)
{
return value.ToString();
}


void Test()

{
int i = 123;

string s = ConvertToString(i);
}

The compîler infers the type of the generic method from the type being
passed to the value parameter. A true int gets passed to the method and
ToString() gets called on the native type with no boxing.

So, you can see, for a little more effort on the part of the develpoer,
boxing can be avoided again.

Joanna

This would now seem to make much more sense. As long as the unnecessary overhead
can be easily avoided, and the purpose of this overhead is to make it a little
easier on the programmer for applications where performance is not important,
then there would be no problem. Now that Generics exist, the fast way is also
just as easy. I was pleasantly surprised to find that managed code in 2005
seemed to perform better that native code under Visual C++ 6.0 on two critical
benchmarks.


--
Joanna Carter [TeamB]
Consultant Software Engineer




.



Relevant Pages

  • Re: how to cast an int to a string?
    ... derived from System.Object and Object class has a ToString() static ... If the data is a reference type you will get the Type name as string ... There may not be any boxing involved. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Roll your own std::vector ???
    ... I assumed boxing occurred here. ... string ConvertToString ... int i = 123; ... The compîler infers the type of the generic method from the type being ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: TextBox.text = int (does boxing happen here)
    ... Boxing does not occur, but you must convert the int to a string. ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
    ... I guess you'd have to understand what boxing means... ... Boxing is the process of converting a literal, like a single space, to the ... a string object) for that literal. ... Mattias Sjögren mattias @ mvps.org ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Looping for string array
    ... This is not boxing. ... In your example the string has been downcasted to an object type, ... because its a virtual method in the object base class that is overriden by ... >>Strings are a reference type, there is NO boxing going on! ...
    (microsoft.public.dotnet.languages.vb)