Reputation: 5778
I have a question regarding this code. I'm a beginner and enjoy learning C#. But then I'm on the topic now called Array which is quite difficult and I need your help. I would like to understand the code.
What I don't understand here is what does the part 1, 2, 3, 4 and 5 here mean?
I don't understand what is the function of "const" and "byte" here?
I would appreciate your explanation?
Thanks & regards;-)
1)
const byte numbers = 5;
byte[] myNumbers = new byte[numbers];
byte additionalNumbers;
Random coincidenceNumbers = new Random();
2)
string yourPassword;
Console.WriteLine("Please enter your password:");
yourPassword = Console.ReadLine();
if (yourPassword != "helloWorld")
{
Console.WriteLine("\nWrong password\n");
return;
}
else
{
Console.WriteLine();
Console.WriteLine("Welcome to my world!");
for (int i=0; i < myNumbers.Length; ++i)
{
myNumbers[i]=(byte)(coincidenceNumbers.Next(1,50));
}
}
3)
additionalNumbers=(byte) (coincidenceNumbers.Next(1,50));
4)
Array.Sort(myNumbers);
Console.WriteLine("\nThe Number is:\n");
5)
foreach (byte elem in myNumbers)
{
Console.WriteLine("\t" + elem);
Console.WriteLine();
Console.WriteLine("Additional Number is: " + additionalNumbers);
}
Upvotes: 1
Views: 239
Reputation: 18013
const is a reserver word meaning that "variable" will not vary, instead, its value will not change, if you try to change
const byte numbers = 5;
numbers = 6; // will fail
byte is a type for storing small numbers
Then,
byte[] myNumbers = new byte[numbers];
creates an array of numbers (5) positions. You can for example, assign values to any position within the array, like this:
myNumbers[0] = 4; // position 1
myNumbers[1] = 45; // position 2
myNumbers[2] = 25; // position 3
myNumbers[3] = 0; // position 4
myNumbers[4] = 12; // position 5
myNumbers[5] = 3; // will fail, array just have 5 positions
[Edit]
additionalNumbers=(byte) (coincidenceNumbers.Next(1,50));
Here, coincidenceNumbers is a Random object, so it will generate random numbers. Its "Next" function will generate an integer number. It receives 2 parameters: minimum value and maximum value. So, here it will generate a random number between 1 and 50.
Integer is very big compared with byte, so there is a "casting"... the integer number will be converted to byte. If integer number less than 255, no problem, in other case you will loss precision
If you try to do this
int x = 500;
byte y = (byte) x;
Console.WriteLine(y); // 244, precision lost
Upvotes: 2
Reputation: 7774
From MSDN:
const:
The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable is constant, which means it cannot be modified.
So for example:
const byte numbers = 5;
You are declaring a constant variable of type 'byte' named numbers with value of 5.
Later on, you will not be able to change the variable numbers like:
numbers = 6; // this is not allowed because it was declared as const
I hope this clarifies your first part of the question.
I suggest going to MSDN and lookup up the terms for the other parts of your question. Try getting into a habit of using this resource for future reference.
Upvotes: 0
Reputation: 5813
I'm guessing that the 1*, 2*, 3*, 4*, and 5* are notations pointing something out to you in the example you're looking at.
const is a modifier that marks the variable as constant (i.e. it will never change)
byte is a data type that will store 1 byte (8 bits) of data.
Edit Well, now I feel stupid...the question wasn't formatted quite the same when I initially read it. Feel free to ignore my answer...
Upvotes: 0
Reputation: 26227
You really should read some beginners tutorial on programming (in general). This has very little to do with the actual Array class.
EDIT: Aaaand there you changed your question...
Upvotes: 0
Reputation: 4665
byte is an integer type that stores values 0..255
const means that the value of variable "numbers" will never change (so it is constant).
Upvotes: 0
Reputation: 38580
You need to read some basic education material on arrays, try MSDN Arrays Tutorial for example.
Upvotes: 1