Yeah I agree with you, as well as for the reasons I've pointed out, it's not
a good idea.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Sebastien Lorion
Sent: Tuesday, November 14, 2006 1:02 AM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Data Structures in .Net?

A solution like this is actually fighting against the way .NET was designed.
Strings are immutable and so you cannot modify a string in-place, you need a
string builder. Looking on the net you will see that there are many benefits
to immutability and actually, not only for strings.

The problem with the suggested code is that it is very inefficient. The net
result is that you will be creating a new string object every single time
you read one of the properties. That might put a lot of stress on the
garbage collector and triggers many Gen0 collections and thus promote
prematurely other objects to an older generation.

On the other hand, by using fixed buffers, you are effectively working
against the garbage collector which tries to defragment memory. If you have
many of these objects in memory, it will be next to impossible to defragment
it. One of the consequences of that is losing locality of reference, one of
the most important factor in performance. On top of that, using unsafe code
requires special permissions on the client.

Both solutions also suffer from the fact that you will use 60 chars arrays
no matter the actual required memory is. "John Smith" is 9 chars, why waste
51 ?

I think one of the point of using such kind of memory layout (ala COBOL) was
to maximize memory and simplify code. As you can, we are far from doing that
now. A better and much simpler solution would be to have 3 fields (first,
last, middle) and concatenate them only when FullName is required. Using a
method "GetFullName()" would further clearly indicate to developers that a
new object is created and so, they might want to put in a temporary variable
and avoid calling that method too many times.

Sébastien

> -----Original Message-----
> From: Discussion of advanced .NET topics. [mailto:ADVANCED-
> [EMAIL PROTECTED] On Behalf Of Jon Rothlander
> Sent: Tuesday, November 14, 2006 1:23 PM
> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> Subject: Re: [ADVANCED-DOTNET] Data Structures in .Net?
> 
> Thanks for your response.  I like your approach this to.  This is the
> kind of thing that I have done in the past to support this sort of
> issue.  Can you tell me why you are not sure this is a good idea?  I
> want to under the issues people have with this because I am working on
> a client's code and I want to be aware of any issues with this approach,
> as it's similar to what I have been doing.
> 
> Your solution is very similar to what I have done in the past to work
> around this issue when supporting legacy applications in .Net.  There's
> allot of work in translating the code when using these approach, which
> is what I am trying to avoid by looking at another option.  However,
> the other options seem to be unsafe, and I'm not sure I want to take
> that leap just yet.
> That's what I'm currently looking at.
> 
> -----Original Message-----
> From: Discussion of advanced .NET topics.
> [mailto:[EMAIL PROTECTED] On Behalf Of Bob
> Provencher
> Sent: Monday, November 13, 2006 9:24 PM
> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> Subject: Re: [ADVANCED-DOTNET] Data Structures in .Net?
> 
> Something like this would work the way you want...
> 
> public class Line
> {
>         const int LineLength = 60;
>         const int FirstNameOffset = 0;
>         const int FirstNameLength = 20;
>         StringBuilder sb = new StringBuilder( LineLength );
> 
>         public string FirstName
>         {
>                 get
>                 {
>                         return sb.ToString( FirstNameOffset,
> FirstNameLength );
>                 }
>                 set
>                 {
>                         char[] inchars;
>                         inchars = value.ToCharArray( 0, value.Length );
>                         for ( int i = 0; i < sb.Length; i++ )
>                         {
>                                 if ( i < value.Length )
>                                 {
>                                         sb.Chars[ i ] = inchars[ i ];
>                                 }
>                                 else
>                                 {
>                                         sb.Chars[ i ] = ' ';
>                                 }
>                         }
>                 }
>         }
> 
> }
> 
> But I'm not sure doing this is a good idea...

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to