Arrays in C#
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.
int array_name= new int;
1) Example of static array:
2) Take five values and display them using single Dimensional array.
3) Accept five values from user and display them using single Dimensional array.
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.
5) Accept values and display them using 2-Dimensional array.
It is similar to double dimensional array but column size changes for each row.
Example of Jagged array:
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.
1) Function containing two arguments:
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.
1) Function containing two arguments and with return keyword:
2) Function using ref keyword(call by reference):
3) Function using optional parameter:
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.
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#