Value Types ارجاعی در C# 7.2
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۱۲/۰۱ ۱۲:۱۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public static int Add(in int number1, in int number2)
{
number1 = 5; // Cannot assign to variable 'in int' because it is a readonly variable
return number1 + number2;
} <ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.10.12" />
</ItemGroup> using BenchmarkDotNet.Attributes;
namespace CS72Tests
{
public struct Input
{
public decimal Number1;
public decimal Number2;
}
[MemoryDiagnoser]
public class InBenchmarking
{
const int loops = 50000000;
Input inputInstance = new Input();
[Benchmark(Baseline = true)]
public decimal RunNormalLoop_Pass_By_Value()
{
decimal result = 0M;
for (int i = 0; i < loops; i++)
{
result = Run(inputInstance);
}
return result;
}
[Benchmark]
public decimal RunInLoop_Pass_By_Reference()
{
decimal result = 0M;
for (int i = 0; i < loops; i++)
{
result = RunIn(in inputInstance);
}
return result;
}
public decimal Run(Input input)
{
return input.Number1;
}
public decimal RunIn(in Input input)
{
return input.Number1;
}
}
} static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<InBenchmarking>(); Method | Mean | Error | StdDev | Scaled | Allocated | ---------------------------- |----------:|---------:|---------:|-------:|----------:| RunNormalLoop_Pass_By_Value | 280.04 ms | 2.219 ms | 1.733 ms | 1.00 | 0 B | RunInLoop_Pass_By_Reference | 91.75 ms | 1.733 ms | 1.780 ms | 0.33 | 0 B |
public static class Factorial
{
public static int Calculate(in this int num)
{
int result = 1;
for (int i = num; i > 1; i--)
result *= i;
return result;
}
} int num = 3;
Console.WriteLine($"(in num) -> {Factorial.Calculate(in num)}");
Console.WriteLine($"(num) -> {Factorial.Calculate(num)}");
Console.WriteLine($"num. -> {num.Calculate()}"); (in num) -> 6 (num) -> 6 num. -> 6
public class CX
{
public void A(Input a)
{
Console.WriteLine("int a");
}
public void A(in Input a)
{
Console.WriteLine("in int a");
}
} public class Y
{
public void Test()
{
var inputInstance = new Input();
var cx = new CX();
cx.A(inputInstance); // The call is ambiguous between the following methods or properties: 'CX.A(Input)' and 'CX.A(in Input)'
}
} public IEnumerable<int> B(in int a) // Iterators cannot have ref or out parameters
{
Console.WriteLine("in int a");
yield return 1;
} public async Task C(in int a) // Async methods cannot have ref or out parameters
{
await Task.Delay(1000);
} using System;
namespace CS72Tests
{
struct MyStruct
{
public int MyValue { get; set; }
public void UpdateMyValue(int value)
{
MyValue = value;
}
}
public static class TestInStructs
{
public static void Run()
{
var myStruct = new MyStruct();
myStruct.UpdateMyValue(1);
UpdateMyValue(myStruct);
Console.WriteLine(myStruct.MyValue);
}
static void UpdateMyValue(in MyStruct myStruct)
{
myStruct.UpdateMyValue(5);
}
}
} static void UpdateMyValue(in MyStruct myStruct)
{
myStruct.MyValue = 5; // Cannot assign to a member of variable 'in MyStruct' because it is a readonly variable
myStruct.UpdateMyValue(5);
} namespace CS8Tests;
public class RefReadonlySample
{
public void Test()
{
var number = 5;
Print(ref number);
Console.WriteLine($"After Print -> Your number is {number}");
// Output:
// Print -> Your number is 5
// After Print -> Your number is 6
}
private void Print(ref int number)
{
Console.WriteLine($"Print -> Your number is {number}");
number++;
}
} private void Print(in int number)
error CS8331: Cannot assign to variable 'number' or use it as the right hand side of a ref assignment because it is a readonly variable
private void Print(ref readonly int number)