473,789 Members | 2,799 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
42 1964
Mark Wilden <Ma********@new sgroups.nospam> wrote:
"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.


Running the code doesn't actually give any guarantee, however. For
instance, if the language specification didn't say what the result of
version 1 was, it might be fine at the moment but produce bogus results
later on.

Looking up array initialization in the spec is the way forward - in
this case, it *does* specify that the new array is filled with 0s to
start with.

--
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 #11
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...

Running the code doesn't actually give any guarantee, however. For
instance, if the language specification didn't say what the result of
version 1 was, it might be fine at the moment but produce bogus results
later on.


You're right, but for all practical purposes, running the code would give
you a quicker answer than posting to a newsgroup. It certainly should be
done just to see what the current behavior of the compiler is. As for
guarantees, there are others than are found in language standards - such as
Microsoft's desire not to break code which (fairly or unfairly) relies on
this behavior.

I was really just commenting on the practice of asking questions when a
better way is to do some experimentation , hit F1 in VS, or (indeed) look up
the issue in the spec. It always makes me think the questioner is trying to
short-circuit an interview question or homework assignment.

But you're right -- the -best- way to solve the problem is neither of the
methods I suggested. :)

///ark
Jun 21 '06 #12
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...

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?


I hear that. It's like initializing members to null, "just to be on the safe
side."

///ark
Jun 21 '06 #13
> Running the code doesn't actually give any guarantee, however. For
instance, if the language specification didn't say what the result of
version 1 was, it might be fine at the moment but produce bogus results
later on.


The language speification DOES say what the result of #1 is; all ints
are initialized to 0. Check the CLS specification if you don't believe
me, but it does state that all number primitives MUST default to 0.

Andy

Jun 22 '06 #14
Ignacio Machin ( .NET/ C# MVP ) ha scritto:
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.


I know, but I was thinking: "ok, and if tomorrow I have to change my array
length and/or change the initial values?".

Bye,
Giulio

Jun 22 '06 #15
Andy <aj*****@alum.r it.edu> wrote:
Running the code doesn't actually give any guarantee, however. For
instance, if the language specification didn't say what the result of
version 1 was, it might be fine at the moment but produce bogus results
later on.


The language speification DOES say what the result of #1 is; all ints
are initialized to 0. Check the CLS specification if you don't believe
me, but it does state that all number primitives MUST default to 0.


That's why I wrote "*if* the language specification didn't say". I then
went on to write this bit which you snipped:

<quote>
Looking up array initialization in the spec is the way forward - in
this case, it *does* specify that the new array is filled with 0s to
start with.
</quote>

--
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 22 '06 #16
Thanks.

Whenever the framework being bigger, the developer should memorize more
rules.

I determined NO 1 choice.

Jun 22 '06 #17
Ahh, sorry Jon, it was late, and I read too fast, and missed the word
if. Sorry about that.

Jun 22 '06 #18
Giulio Petrucci <gi************ *@RIMUOVIspeech village.com> wrote:
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.


I know, but I was thinking: "ok, and if tomorrow I have to change my array
length and/or change the initial values?".


Then tomorrow you make that change. (And tomorrow you might *use* your
intArrayLength variable :) There's no point in including code which
does nothing useful just in *case* you need to change it in a way
you've predicted - it's more likely that you'll need to change it in a
way which you can't predict at this point.

Google for YAGNI for more on this :)

--
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 22 '06 #19
I won't rely on behaviours that I'm not sure at the time I write something.

I search the MSDN library, but can't find anywhere explicitly say this. The
Array Overview said nothing about it too. And everywhere here and there
inside it declares variable like this:

Copied from "Properties overview" :
ms-help://MS.MSDNQTR.2006 JAN.1033/cpguide/html/cpconProperties Overview.htm

[C#]
private int number = 0;
[Visual Basic]
Private number As Integer = 0

So I think there should be a reason for initializing the variables
explicitly.

P.S.: I AM sure now. :)

"Jon Skeet [C# MVP]" <sk***@pobox.co m>
???????:MP***** *************** ****@msnews.mic rosoft.com...
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 23 '06 #20

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

Similar topics

5
1941
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
14858
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
6976
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
1596
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
9511
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
10408
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...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
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
9020
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
6768
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
5417
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
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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.