Array to Function
In C#, “Array to function” refers to passing an array as an argument to a function. This allows the function to operate on the elements of the array. When you pass an array to a function, you are essentially giving that function access to the elements of the array, enabling it to perform operations like reading, modifying, or analysing those elements.
Structure:
public void YourFunctionName(int[] arrayName)
{
// Your function code here
}
Scenario 1: Write a C# program, create a method named DisplayCarParts that takes an array of strings as a parameter and prints each element of the array in the console.
The Main method should create a string array as named carparts and call the DisplayCarParts method with this array.
Print a Simple Array using Array to function
using System;
class Program
{
static void DisplayParts(string[] listofParts)
{
Console.WriteLine(“Car parts:”);
foreach (string part in listofParts)
{
Console.WriteLine(part);
}
}
static void Main()
{
string[] carpartlist = { “Engine” ,”Sensor” ,”starter” };
DisplayParts(carpartlist);
}
}
Output:
Car parts:
Engine
Sensor
Starter
Explanation:
using System;
class Program
{
static void DisplayParts(string[] listofParts)
Defines a static method that takes a string array as a parameter
Console.WriteLine(“Car parts:”);
Prints “Car parts:” to the console
foreach (string part in listofParts)
Iterates through each element in the carparts array. For more details please refer C# For Loop section in this Url : https://iqratechnology.com/academy/c-sharp-training/c-for-loop/
Console.WriteLine(part);
Prints each car part to the console
static void Main()
Defines the Main method, the entry point of the application
string[] carpartlist = { “Engine”, “Sensor”, “starter” };
Create and initialize an array of string with varibale carpartlist
DisplayParts(carpartlist);
Calls the DisplayParts method and passes the carpartlist array as an argument to it
Output:
Car parts:
Engine
Sensor
Starter
Scenario 2: Write a C# program that calculates and displays the addition of any three numbers using a method named Sum.
The Sum method should take any 3 numbers and return their sum.
using System;
class Program
{
static int Sum( int[] Value)
{
int sum = 0;
foreach (int number in Value)
{
sum += number;
}
return sum;
}
static void Main()
{
int[] Value = {2,4,8 };
int ValueofSum = Sum(Value);
Console.WriteLine(ValueofSum);
}
}
Output: 14
Explanation:
using System;
Includes the System namespace, which contains fundamental classes like Console
class Program
Defines a class named Program
static int Sum(int[] Value)
Method that calculates the sum of elements in an integer array
int sum = 0;
Define a varibale named sum and set value 0 to it.
foreach (int number in Value)
Iterates through each element in the Value array. For more details please refer C# For Loop section in this Url : https://iqratechnology.com/academy/c-sharp-training/c-for-loop/
sum += number;
Adds each element number to sum
return sum;
Returns the calculated sum
static void Main()
The entry point of the program
int[] Value = { 2,4,8};
Define an integer array as named Value with elements 2, 4, and 8
int ValueofSum = Sum(Value);
Calls the Sum method with Value as the argument and stores the result in ValueofSum
Console.WriteLine(ValueofSum);
Prints the result (sum of array elements) to the console
Course Video
Course Video English
Task:
1. Sum of Elements:
Create a function SumArray that takes an array of integers and returns the sum of its elements.
Test the function by passing an array of integers and printing the result.
2. Average of Elements:
Create a function AverageArray that takes an array of doubles and returns the average of its elements.
Test the function by passing an array of doubles and printing the result.
3.Find Maximum:
Create a function FindMax that takes an array of integers and returns the maximum value.
Test the function by passing an array of integers and printing the result.
4. Count Occurrences:
Create a function CountOccurrences that takes an array of integers and a target integer, and returns the number of times the target integer occurs in the array.
Test the function by passing an array of integers and a target integer, and printing the result.
5. Filter Even Numbers:
Create a function FilterEvenNumbers that takes an array of integers and returns a new array containing only the even numbers from the original array.
Test the function by passing an array of integers and printing the result.
6. Calculate Median:
Create a function CalculateMedian that takes an array of integers and returns the median value.
Test the function by passing an array of integers and printing the result.
Passing arrays to a function in C# allows you to manipulate and process a collection of values within the function. It provides a way to perform operations like sorting, searching, or aggregating elements in a reusable manner.
Yes, arrays in C# are passed by reference by default. This means any changes made to the array elements inside the function will reflect in the original array.
Common use cases include data processing tasks such as filtering, transforming elements, computing aggregate values, and implementing algorithms like sorting and searching.
Since arrays are passed by reference in C#, there’s no significant performance overhead. However, when working with large arrays, you should be cautious about modifying their contents unintentionally.
If a null array is passed to a function, attempting to access its elements will throw a NullReferenceException. It is good practice to check for null within the function.
- Use descriptive parameter names to indicate the array’s purpose.
- Avoid modifying arrays directly within the function unless necessary.
- Use ReadOnlySpan<T> for performance-critical code to ensure immutability and efficiency.
In C#, you can pass an array to a function by specifying the array type as a parameter. This allows the function to access and manipulate the array passed to it.
Example:
using System;
class Program {
static void DisplayArray(int[] arr) {
foreach (int num in arr) {
Console.WriteLine(num);
}
}
static void Main() {
int[] numbers = { 1, 2, 3, 4, 5 };
DisplayArray(numbers); // Passing array to the function
}
}
In this example, the DisplayArray function takes an array as a parameter and prints each element.
Yes, Iqra Academy offers free video tutorials in Hindi that explain how to pass arrays as function parameters in C#. These tutorials help beginners understand how arrays can be used as parameters and how to manipulate data within functions effectively.
Passing arrays as function parameters in C# is useful because it allows you to modify the array data within the function. It also enables you to perform operations such as sorting, searching, or aggregating data directly in the function, without needing to create a new array or return a value.
You can learn C# array functions and how to pass arrays as function parameters through video tutorials in Hindi, available at Iqra Academy. These tutorials offer step-by-step explanations and practical examples for beginners.
You can perform various operations on an array passed to a function in C#, including:
- Sorting the array using Array.Sort()
- Searching for specific elements with Array.IndexOf()
- Modifying array elements (e.g., adding, removing, or updating values)
- Aggregating values such as sum or average
Yes, you can access a free online course at Iqra Academy, which covers array functions and how to pass arrays as parameters in C#. The course includes practical examples and video tutorials to help you understand these concepts thoroughly.