C Sharp Arrays and Strings
data_type [] array_name;
int [] a;
where
int- data_type
[]- size of array
a- name of the array
where
int- data_type
[]- size of array
a- name of the array
int [] a =new int[10];
This allocates memory to store 10 integer elements.
This allocates memory to store 10 integer elements.
int [] a =new int [10];
a[0]=10;
Note:Array index starts from zero.
a[0]=10;
Note:Array index starts from zero.
int [] a={1,2,3,4};
Also in another way
int [] a=new int[5] {1,2,3,4,5};
Also omiting size
Also in another way
int [] a=new int[5] {1,2,3,4,5};
Also omiting size
int [] a=new int[] {1,2,3,4,5};
using System;
namespace array
{
class Program
{
static void Main(string[] args)
{
int[] myarray = new int[10];
int i,j;
for (i = 1; i <= 5; i++)
{
myarray[i] = i + 10;
}
for (j = 1; j <= 5; j++)
{
Console.WriteLine("Element {0}", j);
}
Console.ReadKey();
}
}
}
Element 1
Element 2
Element 3
Element 4
Element 5
Element 2
Element 3
Element 4
Element 5
Two Dimensional Array
int [,] k;
Three Dimensional Array
string [,,] str;
The Two Dimensional Array is a form table having Rows and Columns.
Above is the Two Dimensional Array with 3 Rows and Columns. 'K' is the name of the Array and the values in brackets are subscripts/indexes. We can access the elements in the Array with the subscripts k[i,j] where I refers to Row and J refer to Column.
The Two Dimensional Array can be initialized as follows.
int [,] k=new int [3,3]={{0,0,0},{0,2,3},{2,4,2}};
Program
using System;
namespace multidimensional_Array
{
class Program
{
static void Main(string[] args)
{
int[,] k = new int[3, 3]{{2,2,4},{3,2,4},{4,3,2}};
int i, j;
Console.WriteLine("The values at 2nd row and 3rd column is a[{0},{1}]:{2}", 2,3,k[1,2]);
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Console.WriteLine("a[{0},{1}] = {2}", i, j, k[i, j]);
}
} Console.ReadLine();
}
}
}
namespace multidimensional_Array
{
class Program
{
static void Main(string[] args)
{
int[,] k = new int[3, 3]{{2,2,4},{3,2,4},{4,3,2}};
int i, j;
Console.WriteLine("The values at 2nd row and 3rd column is a[{0},{1}]:{2}", 2,3,k[1,2]);
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Console.WriteLine("a[{0},{1}] = {2}", i, j, k[i, j]);
}
} Console.ReadLine();
}
}
}
Output
The values at 2nd row and 3rd column is a[{0},{1}]:{2}= a[2,3]:4
a[0,0] = 2
a[0,1] = 2
a[0,2] = 4
a[1,0] = 3
a[1,1] = 2
a[1,2] = 4
a[2,0] = 4
a[2,1] = 3
a[2,2] = 2
a[0,0] = 2
a[0,1] = 2
a[0,2] = 4
a[1,0] = 3
a[1,1] = 2
a[1,2] = 4
a[2,0] = 4
a[2,1] = 3
a[2,2] = 2
string car="Audi";
string color="black";
string vehicle=car+color;
output.
Audi black.
string color="black";
string vehicle=car+color;
output.
Audi black.
char [] vehicle={'A', 'U', 'D', 'I'};
string car=new string(vehicle);
Output.
Audi
string car=new string(vehicle);
Output.
Audi
using System;
namespace strings
{
class Program
{
static void Main(string[] args)
{
string s1 = "Jaguar";
string s2 = "car";
int res = 0;
//Compare two strings
//1 - Not equal
//0 - equal
res = String.Compare(s1,s2);
Console.WriteLine("Result of comparison is " +res);
//check if specified string value is present in String
//True- String contains specified letter
//False-String does contains specified letter
bool b = s1.Contains('a');
Console.WriteLine(b);
//create copy of string
string copy = String.Copy(s1);
Console.WriteLine(copy);
//check whether the strings are equal
//True- equal
//False-Not equal
bool e = copy.Equals(s1);
Console.WriteLine(e);
//return Zero based first occurance of charater specified
int ch=s1.IndexOf('g');
Console.WriteLine(ch);
//check whether the string is null or empty
//True- Yes empty or null string
//False- No Not empty or Null string
bool s = String.IsNullOrEmpty(s1);
Console.WriteLine(s);
Console.ReadKey();
}
}
}
Output.
Result of comparison is 1
True
jaguar
True
2
False
{
class Program
{
static void Main(string[] args)
{
string s1 = "Jaguar";
string s2 = "car";
int res = 0;
//1 - Not equal
//0 - equal
res = String.Compare(s1,s2);
Console.WriteLine("Result of comparison is " +res);
//check if specified string value is present in String
//True- String contains specified letter
//False-String does contains specified letter
bool b = s1.Contains('a');
Console.WriteLine(b);
//create copy of string
string copy = String.Copy(s1);
Console.WriteLine(copy);
//check whether the strings are equal
//True- equal
//False-Not equal
bool e = copy.Equals(s1);
Console.WriteLine(e);
//return Zero based first occurance of charater specified
int ch=s1.IndexOf('g');
Console.WriteLine(ch);
//check whether the string is null or empty
//True- Yes empty or null string
//False- No Not empty or Null string
bool s = String.IsNullOrEmpty(s1);
Console.WriteLine(s);
Console.ReadKey();
}
}
}
Output.
Result of comparison is 1
True
jaguar
True
2
False
C Sharp Arrays and Strings
Reviewed by LanguageExpert
on
August 09, 2017
Rating:
No comments