C# in Parameter with Examples

Shahzad Aslam
2 min readMay 8, 2024

A function in C# is a piece of code that performs a specific task. A function return a value after some manipulation. A function can be called from another function or piece of code. A function is also called method.

C# in Parameter with Examples

in Parameter: Example

using System;
public class Program
{
public static void Main(string[] args)
{
int i = 5;
Console.WriteLine("Before method call: " + i);
ModifyNumber(in i);
Console.WriteLine("After method call: " + i);
}
static void ModifyNumber(in int i)
{
// You cannot modify 'i' here; it's read-only
// i = 10; // This would result in a compile-time error
}
}

Output

Before method call: 5
After method call: 5

In this example, the ModifyNumber method accepts an “in” parameter named i. When you call ModifyNumber(in i), you pass the number variable as an “in” parameter. This means that within the ModifyNumber method, you cannot modify the i parameter, and any attempt to do so will result in a compile-time error.

Summary

The in parameter is particularly useful when you want to pass large or complex data structures to a method without the overhead of copying the data. It’s important to note that the in parameter is read-only within the method, making it safer for scenarios where you don’t want the method to accidentally modify the original data.

Thanks for read.

For more useful tutorials please visit the site: C-Sharp Tutorial and also if you find useful to this article please clap and follow the author by hitting below button.

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.

No responses yet