PC Review


Reply
Thread Tools Rate Thread

Basic C# questions

 
 
=?Utf-8?B?ZGg=?=
Guest
Posts: n/a
 
      16th Nov 2007
Q1:

if (...)
{
byte[] var1 = new byte[4];
var1[0] = 'A';
var1[1] = "B';
var1[2] = 'C';
var1[3] = 'D';
CheckInput(var1); // CheckInput(bytes[] bytes)
...
}

When defining a variable like "var1" in the above code to hold a string of
"ABCD", should it be declared as:

byte[] var1 = new byte[5];

with the last byte to be assigned to '\0' like in C, since var1 will be put
in "CheckInput(bytes[] bytes)" call?


Q2:

if (...)
{
byte[] var1 = new byte[4];
var1[0] = 'A';
var1[1] = "B';
var1[2] = 'C';
var1[3] = 'D';
CheckInput(var1);
...
}

if (...)
{
byte[] var1 = new byte[10];
...
}

Will it be Okay to reuse the var1 this way? Or is there a better to release
var1 for reusing it in the following code?
 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      16th Nov 2007
..net characters are unicode, so you need char[], not byte[]
..net strings are not null-terminated, but this is an implementation
detail anyway... both string and arrays have a .Length property that
tells you what you need

as for re-using it; at the language level they are completely separate
variables; they are scoped to their declaring expression "{...}". The
compiler could choose to use a single backing variable (I haven't
checked), but it doesn't matter.

If you are handling strings, I'd typically use a "string", not a
char[]; this is the exception, for instance when buffering to/from a
stream[reader|writer]. Normally string is fine for the job. Note that
strings are immutable, so if you are going to manipulate it lots then
either char[] or (more often) StringBuilder may be of use.

Marc
 
Reply With Quote
 
Mattias Sjögren
Guest
Posts: n/a
 
      16th Nov 2007
>Q1:
>
>if (...)
>{
> byte[] var1 = new byte[4];
> var1[0] = 'A';
> var1[1] = "B';
> var1[2] = 'C';
> var1[3] = 'D';
> CheckInput(var1); // CheckInput(bytes[] bytes)


Don't you want a char[] instead?



>When defining a variable like "var1" in the above code to hold a string of
>"ABCD", should it be declared as:
>
>byte[] var1 = new byte[5];
>
>with the last byte to be assigned to '\0' like in C, since var1 will be put
>in "CheckInput(bytes[] bytes)" call?


There's no need to zero terminate an array. All arrays keep track of
their length anyway.


>Q2:
>
>if (...)
>{
> byte[] var1 = new byte[4];
> var1[0] = 'A';
> var1[1] = "B';
> var1[2] = 'C';
> var1[3] = 'D';
> CheckInput(var1);
> ...
>}
>
>if (...)
>{
> byte[] var1 = new byte[10];
> ...
>}
>
>Will it be Okay to reuse the var1 this way? Or is there a better to release
>var1 for reusing it in the following code?


Not sure exactly what you're asking here, and what you mean by
"release". You're not reusing the same variable, only the name. var1
in the fist block is different from var1 in the second. Of course it
might be better for other people reading your code if you get a little
more creative in naming your variables, but to the compiler it doesn't
matter.



Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      17th Nov 2007
dh,

This is not basic C# this is basic about computers.

Memory is in bytes, those are and where always adressable as

Displacement + start + index (In the beginning there was not the
displacement)

That we are using in fact pseudos for numeric fields does not mean that this
are true values. Names as: long, integer, numericdecimal, etc are introduced
to overcome the trouble to fill the accumulators and registers.

We are now used to some standards, but that has not for ever been like that.
The Byte is the Raw 8bit format for memory. (Although I have known words
from 24 to 4 bits do a lot of us now assume that it has always been 8 bits).

As you probably know is the now accepted word "string" as standard for an
array of char even very young.

Cor

 
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
Basic Questions don Microsoft Access 4 12th Oct 2009 07:04 AM
Very Basic Questions about Visual Basic =?Utf-8?B?anVzdHBoaWxpcDIwMDM=?= Microsoft Access 3 17th Apr 2007 02:56 AM
Basic RAM Questions Annon Windows XP Hardware 5 12th Jul 2005 12:09 AM
Re: 2 basic questions John Barnett MVP Windows XP Basics 0 14th Sep 2004 06:55 PM
Basic RD Questions frank Windows XP Work Remotely 3 28th Jul 2004 09:50 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:13 AM.