7. การประกาศตัว แปรอาเรย์
ตัวแปรอาเรย์ใช้สำาหรับอ้างถึงอาเรย์
ไวยากรณ์:
<type>[] <name>;
<type>[] <name>;
ตัวอย่าง:
// "scores" refers to an array of integers
// "scores" refers to an array of integers
int[] scores;
int[] scores;
// "grades" refers to an array of characters
// "grades" refers to an array of characters
char[] grades;
char[] grades;
// "names" refers to an array of strings
// "names" refers to an array of strings
string[] names;
string[] names;
14. การส่ง อาเรย์ไ ปยัง เมท็อ ด
เมท็อดต่อไปนี้ทำาการหาผลรวมของ
ทุกค่าข้อมูลทีอยู่ในอาเรย์
่
static double Sum(double[] array) {
static double Sum(double[] array) {
double sum = 0.0;
double sum = 0.0;
foreach (double x in array)
foreach (double x in array)
sum += x;
sum += x;
}
}
return sum;
return sum;
15. การตรวจสอบขนาดขอ
งอาเรย์
By property Le ng th
int[] scores = new int[5];
int[] scores = new int[5];
Console.WriteLine(scores.Length);
Console.WriteLine(scores.Length);
By method Ge tLe ng th
int[] scores = new int[5];
int[] scores = new int[5];
Console.WriteLine(scores.GetLength(0));
Console.WriteLine(scores.GetLength(0));