What Are benefits of using REF keyword in C#?

There are two ways to pass parameters to a method in C#: by value(usual method) and by reference. When passing parameters by reference, the ref modifier is used before the parameters:

void Increment(ref int n)
{
    n++;
}

But what’s the point then that this parameter will be passed by reference?

When you pass a variable to a function NOT by reference, it is copied, that is, it takes up additional memory and additionally loads the system. Of course, when you’re passing small objects like the number 3 or the string “Hello World”, passing parameters like this has little effect. But when you work with large amounts of data (for example, 3D objects) that you do not want to change for a variety of reasons, you only pass the address to the value, thereby once again not loading the computer.

Leave a Reply

Your email address will not be published.