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 1968
"Ian Semmel" <is***********@ NOKUNKrocketcom p.com.au> wrote in message
news:ef******** ******@TK2MSFTN GP03.phx.gbl... To me, initialization of fields to default values just screams "the writer doesn't know C#!"
It doesn't mean any such thing. It means that the coder has a variable that he/she wants initialized to 0.
If she knows the basics of the C# language, then she'll know that if she
wants a field initialized to 0, she doesn't need to (and indeed should not)
use a field initializer.
///ark
Sericinus hunter wrote: Mark Wilden wrote: "Sericinus hunter" <se*****@flash. net> wrote in message news:cz******** ************@ne wssvr21.news.pr odigy.com...
Another thing looks strange. We know now that explicit initialization to default value is redundant and just plain wrong because efficiency suffers.
To me, initialization of fields to default values just screams "the writer doesn't know C#!"
I'm glad to have another reason besides snobbery to deprecate it.
This is documented and supposed to be followed, at least at MS. Then, why compiler does not allow me do things like this: int i; int ii = i; throwing "using unassigned variable" error?
(I assume you're talking about fields, not local variables - in the latter case, i is not initialized.)
Sorry, I just jumped a step ahead without warning... We discussed specification for field initialization, but I am talking about local variables now. Looks like it is initialized. If I write:
int i; i = 1;
and stop in the debugger right after its declaration, I can see it having value 0. And the message is quite clear on that: it says "unassigned ". It is probably due to the language spec vs CLI spec, and designed so just to be safer.
No, it's not initialized.
It has a value (as a variables always has one), and it happens to be
zero in this case, but the value is undefined and could be anything that
happened to be in that memory location on the stack when it was allocated.
Göran Andersson wrote: Sericinus hunter wrote:
.... Sorry, I just jumped a step ahead without warning... We discussed specification for field initialization, but I am talking about local variables now. Looks like it is initialized. If I write:
int i; i = 1;
and stop in the debugger right after its declaration, I can see it having value 0. And the message is quite clear on that: it says "unassigned ". It is probably due to the language spec vs CLI spec, and designed so just to be safer.
No, it's not initialized.
It has a value (as a variables always has one), and it happens to be zero in this case, but the value is undefined and could be anything that happened to be in that memory location on the stack when it was allocated.
This makes sense. Thank you.
Mark Wilden wrote: "Ian Semmel" <is***********@ NOKUNKrocketcom p.com.au> wrote in message news:ef******** ******@TK2MSFTN GP03.phx.gbl...
To me, initialization of fields to default values just screams "the writer doesn't know C#!"
It doesn't mean any such thing. It means that the coder has a variable that he/she wants initialized to 0.
If she knows the basics of the C# language, then she'll know that if she wants a field initialized to 0, she doesn't need to (and indeed should not) use a field initializer.
///ark
No, in any language, the (equivalent) statement of
int x;
means that this variable exists, but it has a 'don't care' value, the
implication being that it will be set later on.
int x = 0;
means that it starts off with a definite value.
Similarly, parentheses should always be used in expressions without depending on
a language-specific hierarchy of operator precedence. It is an old 'C' hacker
convention to help the compiler by reducing the number of characters in source code.
It is a good idea to use a commonality of coding styles across all languages in
my opinion.
Ian Semmel wrote: Mark Wilden wrote:
.... If she knows the basics of the C# language, then she'll know that if she wants a field initialized to 0, she doesn't need to (and indeed should not) use a field initializer.
No, in any language, the (equivalent) statement of
int x;
means that this variable exists, but it has a 'don't care' value, the implication being that it will be set later on.
int x = 0;
means that it starts off with a definite value.
Similarly, parentheses should always be used in expressions without depending on a language-specific hierarchy of operator precedence. It is an old 'C' hacker convention to help the compiler by reducing the number of characters in source code.
It is a good idea to use a commonality of coding styles across all languages in my opinion.
Just to make sure, we are talking about the same thing. What Mark says
(and what I agree with him upon) relates to type fields, not to local
variables. Fields have initializers, this is a feature of the language.
Fields are initialized automatically using processor instructions to
fill memory blocks with zeros, thus very efficiently. It would be just
silly to reassign a field to the same value again and besides using
less efficient higher level commands.
And some feel it is even more silly to not initialize such data. From a
managers perspective you don't know who is going to maintain that code
several years down the road when a whole new set of languages is being used
and those people may not know a thing about C#. Many perspectives can be
valid. What Albert wrote many years ago is still true, "It is all relative".
Ian Semmel <is***********@ NOKUNKrocketcom p.com.au> wrote:
<snip> It is a good idea to use a commonality of coding styles across all languages in my opinion.
I couldn't disagree more.
Many languages have different coding conventions in many ways - naming
being the most obvious example. Some idioms from some languages *just
don't work* in others, or effectively fight against the language.
IMO it's worth going with the idioms, specification and conventions of
each language you use - try to avoid writing in one language with an
"accent" of another.
--
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
<ne************ ***@charter.net > wrote: And some feel it is even more silly to not initialize such data. From a managers perspective you don't know who is going to maintain that code several years down the road when a whole new set of languages is being used and those people may not know a thing about C#. Many perspectives can be valid. What Albert wrote many years ago is still true, "It is all relative".
While it's a valid viewpoint in some ways, I'd argue that anyone who
doesn't know C# to at least a reasonable extent shouldn't be
maintaining it. It's likely to be unproductive in the long run. Suppose
someone is used to C++ and starts maintaining C# - they might assume
that the RAII idiom works in C#, and so never calls Dispose on any
streams etc. Not a good idea.
I think it's good to try to avoid relying on people knowing really
obscure bits of the spec, but some basic knowledge is reasonable.
If we agree on that, the question is really whether or not default
values count as basic knowledge. Personally I'd say they are, but I
wouldn't be surprised to find people disagreeing.
--
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
"Ian Semmel" <is***********@ NOKUNKrocketcom p.com.au> wrote in message
news:eH******** ******@TK2MSFTN GP05.phx.gbl... No, in any language, the (equivalent) statement of
int x;
means that this variable exists, but it has a 'don't care' value, the implication being that it will be set later on.
In C#, when x is a field, means that the variable's value is set to 0.
Similarly, parentheses should always be used in expressions without depending on a language-specific hierarchy of operator precedence. It is an old 'C' hacker convention to help the compiler by reducing the number of characters in source code.
It is a good idea to use a commonality of coding styles across all languages in my opinion.
I can't see any possible basis for that opinion. By that logic, we wouldn't
use OOP, since C doesn't support it.
<ne************ ***@charter.net > wrote in message
news:Wc******** *******@fe06.lg a... And some feel it is even more silly to not initialize such data. From a managers perspective you don't know who is going to maintain that code several years down the road when a whole new set of languages is being used and those people may not know a thing about C#.
So you're saying we ought to write C# so that it can be understood by people
who do not know a thing about C#? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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
|
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...
|
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...
|
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:
| |
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.
|
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".
|
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;
}
|
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();
}
|
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...
|
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,...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |