PC Review


Reply
Thread Tools Rate Thread

About best coding Style of C#

 
 
kiplring
Guest
Posts: n/a
 
      21st Jun 2006
1.
int[] intArr = new int[255];

2.
int[] intArr = new int[255];
rgnNumberArr.Initialize();

3.
int[] intArr = new int[255];
for( int i=0; i<intArr .lenght; i++)
{
intArr [i] = 0;
}

intArr allways initilized with 0 value?
or should I initialize it?

choose best code 1,2,3?

 
Reply With Quote
 
 
 
 
Giulio Petrucci
Guest
Posts: n/a
 
      21st Jun 2006
kiplring ha scritto:
> 1.
> int[] intArr = new int[255];
>
> 2.
> int[] intArr = new int[255];
> rgnNumberArr.Initialize();
>
> 3.
> int[] intArr = new int[255];
> for( int i=0; i<intArr .lenght; i++)
> {
> intArr [i] = 0;
> }
>
> intArr allways initilized with 0 value?
> or should I initialize it?
>
> choose best code 1,2,3?


uhm... I'm not what you can say a "good developer" :-) but I should do like
this:

int intArrayLength = 255;
int[] intArr = new int[255];
for( int i=0; i<intArr .lenght; i++)
{
intArr [i] = 0;
}


bye,
Giulio - Italia
 
Reply With Quote
 
Lau Lei Cheong
Guest
Posts: n/a
 
      21st Jun 2006
I'm not a "good developer" either, but I'll vote for no.3.

Note that the MSDN documentation explicitly stated that "if the value type
does not have a default constructor, the Array is not modified.", so no.2 is
no better than no.1.

btw, I somehow remember I've read that the .NET framework will preinitialize
interger array to zero for you but you should never rely on this
behaviour... I'm not so sure about this...

"kiplring" <(E-Mail Removed)>
???????:(E-Mail Removed)...
> 1.
> int[] intArr = new int[255];
>
> 2.
> int[] intArr = new int[255];
> rgnNumberArr.Initialize();
>
> 3.
> int[] intArr = new int[255];
> for( int i=0; i<intArr .lenght; i++)
> {
> intArr [i] = 0;
> }
>
> intArr allways initilized with 0 value?
> or should I initialize it?
>
> choose best code 1,2,3?
>



 
Reply With Quote
 
Barry Kelly
Guest
Posts: n/a
 
      21st Jun 2006
"kiplring" <(E-Mail Removed)> wrote:

> 1.
> int[] intArr = new int[255];


All dynamic allocation with 'new' is initialized to whatever '0' means
for that type. For this array, that means the whole array will be zeroed
out.

> 2.
> int[] intArr = new int[255];
> rgnNumberArr.Initialize();


I don't know what rgnNumberArr has to do with intArr, so I can't comment
on this code.

Array.Initialize() is for value types that have constructors (i.e. from
C++); you typically don't need to call it in a C# application. Also, the
C++ folks have been toning down their guarantees on .NET about default
construction of value types, so now it's even less necessary.

> 3.
> int[] intArr = new int[255];
> for( int i=0; i<intArr .lenght; i++)
> {
> intArr [i] = 0;
> }


This loop is redundant.

-- Barry

--
http://barrkel.blogspot.com/
 
Reply With Quote
 
Alun Harford
Guest
Posts: n/a
 
      21st Jun 2006
"kiplring" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 1.
> int[] intArr = new int[255];
>
> 2.
> int[] intArr = new int[255];
> rgnNumberArr.Initialize();
>
> 3.
> int[] intArr = new int[255];
> for( int i=0; i<intArr .lenght; i++)
> {
> intArr [i] = 0;
> }


If you get an int[] by calling new, you'll get an array full of 0s.
If you do it from unmanaged code, you'll get whatever junk was in that
memory before.

Alun Harford


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      21st Jun 2006
Hi,

"kiplring" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 1.
> int[] intArr = new int[255];


The way to go !

> 2.
> int[] intArr = new int[255];
> rgnNumberArr.Initialize();


What is rgnNumberArr ?
In anyway calling Initialize is not needed, see what MSDN says about it:

This method is designed to help compilers support value-type arrays; most
users do not need this method. It must not be used on reference-type arrays.

If the Array is not a value-type Array or if the value type does not have a
default constructor, the Array is not modified.

The value-type Array can have any lower bound and any number of dimensions.

CAUTION You can use this method only on value types that have
constructors; however, value types that are native to C# do not have
constructors.

> 3.
> int[] intArr = new int[255];
> for( int i=0; i<intArr .lenght; i++)
> {
> intArr [i] = 0;
> }


Not needed, it will be done for you.



--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      21st Jun 2006
Hi,


> int intArrayLength = 255;
> int[] intArr = new int[255];
> for( int i=0; i<intArr .lenght; i++)
> {
> intArr [i] = 0;
> }


You do not need to initialize a value type array, it will ALWAYS have a
value (contrary to what happen with a reference typed array which will be
null ), this is why a DateTime cannot be null.


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


 
Reply With Quote
 
Andy
Guest
Posts: n/a
 
      21st Jun 2006
> btw, I somehow remember I've read that the .NET framework will preinitialize
> interger array to zero for you but you should never rely on this
> behaviour... I'm not so sure about this...


You CAN rely on ints being initialized to 0; .Net guarentees that this
will happen.

 
Reply With Quote
 
Mark Wilden
Guest
Posts: n/a
 
      21st Jun 2006
"kiplring" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 1.
> int[] intArr = new int[255];
>
> 2.
> int[] intArr = new int[255];
> rgnNumberArr.Initialize();
>
> 3.
> int[] intArr = new int[255];
> for( int i=0; i<intArr .lenght; i++)
> {
> intArr [i] = 0;
> }
>
> intArr allways initilized with 0 value?
> or should I initialize it?


I think the best way to solve this problem is 1) Look up Array.Initialize()
to see what it does, and 2) Run the code to see what it produces. Much
quicker than asking people.

///ark


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Jun 2006
Lau Lei Cheong <(E-Mail Removed)> wrote:
> btw, I somehow remember I've read that the .NET framework will preinitialize
> interger array to zero for you but you should never rely on this
> behaviour... I'm not so sure about this...


Why would you "never rely on this behaviour"? Which other pieces of
behaviour which are documented in the language specification should you
not rely on?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Coding style =?Utf-8?B?U2VyZ2V5IFp1eWV2?= Microsoft VB .NET 52 27th Jul 2006 01:02 PM
Best coding style for c#( 3rd) kiplring Microsoft C# .NET 10 5th Jul 2006 08:50 AM
Coding Style =?Utf-8?B?QXJuZSBHYXJ2YW5kZXI=?= Microsoft Dot NET Framework 7 24th Jun 2006 01:35 AM
About Best Coding style of c# kiplring Microsoft C# .NET 1 17th Jun 2006 08:52 AM
Looking for feedback on coding style Dgates Microsoft C# .NET 1 20th Aug 2004 11:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:10 AM.