Basic C# questions

G

Guest

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?
 
M

Marc Gravell

..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
 
M

Mattias Sjögren

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
 
C

Cor Ligthert[MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top