В классе general_form я создаю экземпляр ReadyLoad.
internal class ReadyLoad {
private general_form gm_Link;
public ReadyLoad() {
gm_Link = new general_form();
}
}
new ReadyLoad
- внутри него вызывается new general_form()
, а внутри него также вызывается конструктор new ReadyLoad()
и так, пока стек не переполнится.Можно ли как-то "сжать" все это дело?
Ведь часть исходного кода порой можно глянуть сторонними инструментами.
public static int Sum(int[] numbers, int start, int count) {
if(count < 0 || count > numbers.Length)
throw new ArgumentOutOfRangeException(nameof(count));
if(start < 0 || start >= numbers.Length || start + count > numbers.Length)
throw new ArgumentOutOfRangeException(nameof(start));
return count == 0
? 0
: numbers[start] + Sum(numbers, start+1, count-1); // Вот тут наша рекурсия
}
enum SomeEnum {
A,
B
}
var text = "B";
var enumValue = Enum.Parse<SomeEnum>(text); // SomeEnum.B
var enumValue = (SomeEnum) Enum.Parse(typeof(SomeEnum), text);
throw ex;
throw new MyEx(ex);
var employeeList = objListList[0].Select(x=>x as Employee).ToList();
Я проверил на 10 млн объектов и получилось что foo is null в 20 раз быстрее чем foo == null. Поэтому и интересно какой способ самый быстрый.
public class Benchmark
{
private static readonly object? Obj = new();
[Benchmark]
public bool EqualityOperator() => Obj == null;
[Benchmark]
public bool PatternMatching() => Obj is null;
[Benchmark] public bool ComplexPatterMatching() => Obj is not { };
[Benchmark] public bool ConstantReturn() => false;
[Benchmark] public bool EqualsCall() => Obj!.Equals(null);
[Benchmark] public bool ReferenceEqualsCall() => ReferenceEquals(Obj, null);
}
.method public hidebysig instance bool
EqualityOperator() cil managed
{
.custom instance void [BenchmarkDotNet.Annotations]BenchmarkDotNet.Attributes.BenchmarkAttribute::.ctor()
= (01 00 00 00 )
.maxstack 8
// [10 39 - 10 50]
IL_0000: ldsfld object Benchmark::Obj
IL_0005: ldnull
IL_0006: ceq
IL_0008: ret
} // end of method Benchmark::EqualityOperator
.method public hidebysig instance bool
PatternMatching() cil managed
{
.custom instance void [BenchmarkDotNet.Annotations]BenchmarkDotNet.Attributes.BenchmarkAttribute::.ctor()
= (01 00 00 00 )
.maxstack 8
// [12 38 - 12 49]
IL_0000: ldsfld object Benchmark::Obj
IL_0005: ldnull
IL_0006: ceq
IL_0008: ret
} // end of method Benchmark::PatternMatching
.method public hidebysig instance bool
ComplexPatterMatching() cil managed
{
.custom instance void [BenchmarkDotNet.Annotations]BenchmarkDotNet.Attributes.BenchmarkAttribute::.ctor()
= (01 00 00 00 )
.maxstack 8
// [13 56 - 13 70]
IL_0000: ldsfld object Benchmark::Obj
IL_0005: ldnull
IL_0006: cgt.un
IL_0008: ldc.i4.0
IL_0009: ceq
IL_000b: ret
} // end of method Benchmark::ComplexPatterMatching
.method public hidebysig instance bool
EqualsCall() cil managed
{
.custom instance void [BenchmarkDotNet.Annotations]BenchmarkDotNet.Attributes.BenchmarkAttribute::.ctor()
= (01 00 00 00 )
.maxstack 8
// [15 45 - 15 62]
IL_0000: ldsfld object Benchmark::Obj
IL_0005: ldnull
IL_0006: callvirt instance bool [System.Runtime]System.Object::Equals(object)
IL_000b: ret
} // end of method Benchmark::EqualsCall
.method public hidebysig instance bool
ReferenceEqualsCall() cil managed
{
.custom instance void [BenchmarkDotNet.Annotations]BenchmarkDotNet.Attributes.BenchmarkAttribute::.ctor()
= (01 00 00 00 )
.maxstack 8
// [16 54 - 16 80]
IL_0000: ldsfld object Benchmark::Obj
IL_0005: ldnull
IL_0006: ceq
IL_0008: ret
} // end of method Benchmark::ReferenceEqualsCall
| Method | Mean | Error | StdDev | Median |
|---------------------- |----------:|----------:|----------:|----------:|
| EqualityOperator | 0.0000 ns | 0.0000 ns | 0.0000 ns | 0.0000 ns |
| PatternMatching | 0.0300 ns | 0.0164 ns | 0.0145 ns | 0.0271 ns |
| ComplexPatterMatching | 0.0401 ns | 0.0327 ns | 0.0376 ns | 0.0267 ns |
| ConstantReturn | 0.0000 ns | 0.0000 ns | 0.0000 ns | 0.0000 ns |
| EqualsCall | 1.3787 ns | 0.0437 ns | 0.0409 ns | 1.3770 ns |
| ReferenceEqualsCall | 0.0000 ns | 0.0000 ns | 0.0000 ns | 0.0000 ns |
| Method | Mean | Error | StdDev |
|---------------------- |----------:|----------:|----------:|
| EqualityOperator | 2.5751 ns | 0.0062 ns | 0.0049 ns |
| PatternMatching | 2.5682 ns | 0.0073 ns | 0.0065 ns |
| ComplexPatterMatching | 2.6456 ns | 0.0744 ns | 0.0696 ns |
| ConstantReturn | 0.0065 ns | 0.0044 ns | 0.0035 ns |
| EqualsCall | 4.6958 ns | 0.0337 ns | 0.0282 ns |
| ReferenceEqualsCall | 2.9525 ns | 0.0667 ns | 0.0557 ns |
Могу ли я использовать свой опыт C# .NETFramework 4.8 в остальных реализациях?
В интернете есть 3 версии языка программирования