Arrays in C#
Introduction:
Arrays are used to store multiple values in one variable. Arrays are used to store similar values accordingly to given datatype. Array variable can contain all integers, all characters and all float values. Array index value start with '0'. C# contains static array and dynamic array. Static array means memory will be preserved at compile time. Dynamic array means memory will be preserved at runtime. While creating array variable, if new operator is used then it is called dynamic array.
Syntax:
int[] array_name= new int[10];
Examples:
1) Example of static array:
using System; namespace Static_array { class Program { static void Main(string[] args) { int[] a = { 10, 20, 30 }; Console.WriteLine("{0},{1},{2}", a[0], a[1], a[2]); Console.Read(); } } }
2) Take five values and display them using single Dimensional array.
using System; namespace Array { class Program { static void Main(string[] args) { int[] a=new int[5]; a[0] = 5; a[1] = 10; a[2] = 15; a[3] = 20; a[4] = 25; Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}", a[0],a[1],a[2],a[3],a[4]); Console.Read(); } } }
Output:
3) Accept five values from user and display them using single Dimensional array.
using System; namespace Array { class Program { static void Main(string[] args) { int[] a=new int[5]; Console.WriteLine("Enter values:"); for (int i = 0; i < 5; i++) { a[i] = int.Parse(Console.ReadLine()); } for (int i = 0; i < 5; i++) { Console.WriteLine(a[i]); } Console.Read(); } } }
Output:
foreach loop:
Foreach loop is used with arrays, collections and generics. If any variable contains set of values and if you do not know the maximum value or ending value then foreach loop is used to display all the values.
4) Accept five names and display them using single Dimensional array.
using System; namespace foreach_loop { class Program { static void Main(string[] args) { String[]str=new String[5]; Console.WriteLine("Enter Names"); for(int i=0;i<5;i++) { str[i]=Console.ReadLine(); } foreach (String S in str) { Console.WriteLine(S); } Console.Read(); } } }
Output:
5) Accept values and display them using 2-Dimensional array.
using System; namespace 2DArray { class Program { static void Main(string[] args) { int[,] a = new int[3, 3]; int i, j; Console.WriteLine("Enter value"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { a[i, j] = int.Parse(Console.ReadLine()); } } for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write(a[i, j] + "\t"); } Console.WriteLine(); } Console.Read(); } } }
Output:
Jagged Array:
It is similar to double dimensional array but column size changes for each row.
Example of Jagged array:
using System; namespace JaggedArray { class Program { static void Main(string[] args) { int[][] a = new int[3][]; a[0] = new int[3]; a[1] = new int[4]; a[2] = new int[5]; a[0] = new int[] { 10, 20, 30 }; a[1] = new int[] { 10, 20, 30, 50 }; a[2] = new int[] { 10, 20, 30, 40, 50 }; for (int i = 0; i < 3; i++) { Console.Write(a[0][i]+"\t"); } Console.WriteLine(); for (int i = 0; i < 4; i++) { Console.Write(a[1][i] + "\t"); } Console.WriteLine(); for (int i = 0; i < 5; i++) { Console.Write(a[2][i] + "\t"); } Console.Read(); } } }
Output:
Method/Function in C#
Method is used to execute set of statements as a block. It is used to call from a specific task when it is executed. Method will have return type and will return value.
Examples:
1) Function containing two arguments:
using System; namespace Sum { class Program { static void Sum(int a, int b) { Console.WriteLine("Sum is:" + (a + b)); } static void Main(string[] args) { Sum(10, 20); // Calling Function Console.Read(); } } }
Output:
Note:
Function can be called in two ways:
1) Call by value: While calling the function or while executing the function, if values are given as arguments then it is called call by value.
2) Call by Reference: While calling the function, if the reference of the value or memory address of the value is given as argument then it is called call by reference.
Examples:
1) Function containing two arguments and with return keyword:
using System; namespace Demo { class Program { static int Display(int a, int b) { return (a + b); } static void Main(string[] args) { Console.WriteLine(Display(10, 20)); Console.Read(); } } }
Output:
2) Function using ref keyword(call by reference):
using System; namespace Demo { class Program { static void Print( ref int a,ref int b) { Console.WriteLine("Sum is:" + (a + b)); } static void Main(string[] args) { int x = 5, y = 10; Print(ref x, ref y); Console.Read(); } } }
Output:
3) Function using optional parameter:
using System; namespace Demo { class Program { static void getResult(int a, int b = 10) { Console.WriteLine("Sum is:" + (a + b)); } static void Main(string[] args) { getResult(10); getResult(10, 15); Console.Read(); } } }
Output:
Create function using named parameters:
While creating the function the order of arugments is given. The same order of arguments should be given while calling or executing the function, By using named parameter while calling the function ,the order of arguments can be changed.
Example:
using System; namespace Demo { class Program { static void PrintVal(int a, string s) { Console.WriteLine(a+" "+s); } static void Main(string[] args) { PrintVal(s: "Kamlesh", a: 1); Console.Read(); } } }
Output:
In this way, we have learned Arrays and Function with examples in this article. I hope this will help beginners to understand Arrays and Function.
You may also be interested in...
- Slack Integration with C#
- Xamarin Forms: Getting Started
- Part 1: Introduction to C#
- Part 2: Arrays and Function in C#
- Part 3: OOPs Concepts in C#
- Part 4: Constructor/Destructor in C#
- Part 5: Abstract class in C#
- Part 6: Indexer,Delegates, Anonymous Method, Lambda expression in C#
- Part 7: Collections in C#
- Part 8: Generics in C#
- Part 9: Attributes in C#
- Part 10: Sealed/Partial class in C#
- Part 11: MultiThreading in C#
Happy Coding!
Discuss about post