473,776 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About best coding Style of C#

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

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

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?

Jun 21 '06 #1
42 1961
kiplring ha scritto:
1.
int[] intArr = new int[255];

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

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
Jun 21 '06 #2
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" <ki********@hot mail.com>
???????:11***** *************** **@g10g2000cwb. googlegroups.co m...
1.
int[] intArr = new int[255];

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

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?

Jun 21 '06 #3
"kiplring" <ki********@hot mail.com> 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.In itialize();
I don't know what rgnNumberArr has to do with intArr, so I can't comment
on this code.

Array.Initializ e() 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/
Jun 21 '06 #4
"kiplring" <ki********@hot mail.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
1.
int[] intArr = new int[255];

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

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
Jun 21 '06 #5
Hi,

"kiplring" <ki********@hot mail.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
1.
int[] intArr = new int[255];
The way to go !
2.
int[] intArr = new int[255];
rgnNumberArr.In itialize();
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
Jun 21 '06 #6
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
Jun 21 '06 #7
> 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.

Jun 21 '06 #8
"kiplring" <ki********@hot mail.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
1.
int[] intArr = new int[255];

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

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.Initializ e()
to see what it does, and 2) Run the code to see what it produces. Much
quicker than asking people.

///ark
Jun 21 '06 #9
Lau Lei Cheong <le****@yehoo.c om.hk> 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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1940
by: SungSoo, Han | last post by:
There are two coding style when python import module. (1) import sys (2) from import sys (or from import *) I prefer (1) style. Because it's easy to read, understand module in any category .. Which prefer ? Are there any difference of the 2 styles.
125
14850
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
144
6955
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force everybody else to write code the way he likes it, not for any rational reason, but simply for the...
4
2015
by: Josh Golden | last post by:
i lead a small development team (based on some of my posts that might cause some people to choke themselves, but have no fear, i am NOT the lead developer, the people on my team are great - i'm just the manager) for my company. although we attempt to use good practices for development, we have no real experience in documentation of a project _before_ it's coded. Are there any great books out there on how to document a project before we...
9
2117
by: Rhino | last post by:
I've been updating some CSS today and got one odd error from the validator at http://jigsaw.w3.org/css-validator/. Every time I had 'background: transparent;' (or background-color: transparent;) associated with a selector, it gave me this error message: Line : 30 (Level : 1) You have no background-color with your color : #navlist li a Here's the CSS fragment that it doesn't like:
1
1463
by: kiplring | last post by:
List<string> effectList = new List<string>(); effectList.Clear(); effectList = null; using (List<string> effectList = new List<string>()) { } If there are so many calls, I should save as much memory as I can.
10
1430
by: kiplring | last post by:
I made a input window derived from System.Windows.Forms.Form. What I use is just one property "inputWindow.Message". But System.Windows.Forms.Form has so many properties, functions, evnets. So I want to hide all things except the one property "inputWindow.Message".
13
1594
by: charpour | last post by:
Hello, in the program below: #include <stdio.h> int main(void) { int x = 1, y = 2, z; z = x+++y; return 0; }
12
1481
by: Matt B | last post by:
I was just wondering if there is a "best" choice from the following couple of ways of returning a value from a method: 1) private HashAlgorithm GetSpecificHashAlgorithm(string hashString){ if (hashString == "MD5") { return System.Security.Cryptography.MD5.Create(); } else { return System.Security.Cryptography.SHA512.Create(); }
0
9628
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10292
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5368
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.