Arrays in Csharp
Arrays in Csharp
Declaring Arrays
Arrays are reference type variables whose creation involves two
steps:
Declaration
data_type[] arrayName;
In Above syntax:
Initializing Arrays
The new keyword allocates memory to the array and values can
then be assigned to the array.
The following table lists the default values for some of the
widely used data types:
int 0
float 0.0
double 0.0
char '\0'
string Null
In Above Syntax,
size-value: Specifies the number of elements in the array. You
can specify a variable of type int that stores the size of the array
instead of directly specifying a value.
number[0] = 11;
number[1] = 22;
number[2] = 33;
number[3] = 44;
number[4] = 55;
In Above Syntax,
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArraysDemo
class Program
//Console.WriteLine(myArray.Length);
//{
// Console.WriteLine(myArray[i]);
//}
//foreach (string name in myArray)
//{
// Console.WriteLine(name);
//}
//myArray[0] = 10;
//myArray[1] = 20;
//myArray[2] = 30;
//myArray[3] = 40;
//Console.WriteLine(myArray[0]);
//Console.WriteLine(myArray[1]);
//Console.WriteLine(myArray[2]);
//Console.WriteLine(myArray[3]);
Console.ReadLine();
}
Source Code - Example 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Arrays_Demo
class Program
names[i] = name;
}
Console.WriteLine("----------------");
Console.WriteLine(name);
//foreach(string i in names)
//{
// Console.WriteLine(i);
//}
//{
// Console.WriteLine(names[i]);
//}
// 2 types of arrays
// single dimensional array
//names[1] = "Zubia";
//names[2] = "Saad";
//Console.WriteLine(names[0]);
//Console.WriteLine(names[1]);
//Console.WriteLine(names[2]);
// 4 types of loops
// for loop
// while loop
// do while loop
//nums[0] = 11;
//nums[1] = 22;
//nums[2] = 33;
//nums[3] = 44;
//Console.WriteLine(nums[2]);
Console.ReadLine();