473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Basic C# questions

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?
Nov 16 '07 #1
2 1157
..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
Nov 16 '07 #2
>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.
Nov 16 '07 #3

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

Similar topics

2
by: Steven O. | last post by:
First, this may not be the correct newsgroup. I have some relatively basic questions on SQL. I tried to find a newsgroup that was specifically just about SQL, and was surprised to find that all...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
3
by: Jim H | last post by:
If there is a site someone can point me to that answers such basic questions, rather than taking up support's time posting answers, please let me know. I've developed C# .NET, unmanaged C++, and...
2
by: Fay Yocum | last post by:
BEWARE beginner questions!! I have some experience in Access but never as much as I want or need. I have decided to get in on VB.Net. I would only rate myself in Access as a...
0
by: software2006 | last post by:
ASP And Visual Basic Interview questions and answers I have listed over 100 ASP and Visual Basic interview questions and answers in my website...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.