« Parameter modifiers » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
 
(15 versions intermédiaires par le même utilisateur non affichées)
Ligne 23 : Ligne 23 :
// if the parameter is overwritten (the initial reference is overwritten), it does affect the initial object
// if the parameter is overwritten (the initial reference is overwritten), it does affect the initial object
void Method3(ref Item item) => item = new("new name 3");
void Method3(ref Item item) => item = new("new name 3");
</kode>
= [https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/method-parameters#ref-readonly-modifier ref readonly] =
Pass a parameter by reference and make it readonly.<br>
Useful when you have a big value type and you want to pass only its reference for performances reason, but you don't want it to be overwritten.
<kode lang='cs'>
var bigStruct = new BigStruct("name");
Method(ref bigStruct);
void Method(ref readonly BigStruct bigStruct)
{
    bigStruct = new BigStruct("new name"); // you can't assign a readonly variable
    bigStruct.Name = "new name";          // you can modify the object properties
}
int[] array = [ 1, 2, 3, 4 ];
var bigStruct = new BigStruct(array);
ref readonly int element = ref bigStruct.GetElement(2); // 3
element = 5; // you can't assign a readonly variable
ref readonly int GetElement(int index) => ref data[index];
</kode>
{{info | If the variable is a readonly variable, you must use the {{boxx|in}} modifier while calling the method.}}
= [https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/method-parameters#in-parameter-modifier in] =
Create a temporary variable for the argument and pass the argument by readonly reference.
<kode lang='cs'>
var i = 10;
Method(in i); // in is optional in th calling method
void Method6(in int i) // readonly copy of the initial parameter
{
    i = 5; // you can't assign a readonly variable
}
</kode>
= [https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/method-parameters#out-parameter-modifier out] =
Allow to initialize the parameter in the method.
<kode lang='cs'>
Method5(out var j); // 5
void Method5(out int i) => i = 5;
</kode>
= [https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/method-parameters#params-modifier params] =
A parameter which allows a comma-separated list of arguments or a collection of arguments.
<kode lang='cs'>
Method(1, 2, 3);
int[] a = [1, 2, 3];
Method(a);
void Method(params int[] integers)
{ }
</kode>
</kode>

Dernière version du 24 mai 2024 à 15:46

ref

Pass a parameter by reference.

Cs.svg
// value type
var i = 10;
Method0(i);     // 10
Method1(ref i); // 11

void Method0(int i) => i++;     // pass the value type parameter by value (copy)
void Method1(ref int i) => i++; // pass the value type parameter by reference

// reference type
var item = new Item("init name");
Method2(item);     // init name
Method3(ref item); // new name 3

// pass the reference type parameter by value: the reference is passed by value (copy)
// if the parameter is overwritten (the copy of the reference is overwritten), it doesn't affect the initial object
void Method2(Item item) => item = new("new name 2");

// pass the reference type parameter by reference: the reference is passed by reference
// if the parameter is overwritten (the initial reference is overwritten), it does affect the initial object
void Method3(ref Item item) => item = new("new name 3");

ref readonly

Pass a parameter by reference and make it readonly.
Useful when you have a big value type and you want to pass only its reference for performances reason, but you don't want it to be overwritten.

Cs.svg
var bigStruct = new BigStruct("name");
Method(ref bigStruct);

void Method(ref readonly BigStruct bigStruct)
{
    bigStruct = new BigStruct("new name"); // you can't assign a readonly variable
    bigStruct.Name = "new name";           // you can modify the object properties
}

int[] array = [ 1, 2, 3, 4 ];
var bigStruct = new BigStruct(array);
ref readonly int element = ref bigStruct.GetElement(2); // 3
element = 5; // you can't assign a readonly variable

ref readonly int GetElement(int index) => ref data[index];
If the variable is a readonly variable, you must use the in modifier while calling the method.

in

Create a temporary variable for the argument and pass the argument by readonly reference.

Cs.svg
var i = 10;
Method(in i); // in is optional in th calling method

void Method6(in int i) // readonly copy of the initial parameter
{
    i = 5; // you can't assign a readonly variable
}

out

Allow to initialize the parameter in the method.

Cs.svg
Method5(out var j); // 5

void Method5(out int i) => i = 5;

params

A parameter which allows a comma-separated list of arguments or a collection of arguments.

Cs.svg
Method(1, 2, 3);
int[] a = [1, 2, 3];
Method(a);

void Method(params int[] integers)
{ }