c# - 使用 "constructor rights"从构造函数调用方法

标签 c# constructor inline

我有成员(member)。该成员仅由构造函数分配,为什么我认为它可以设为只读。 这个类有几个构造函数。

有没有办法只分配一次该成员,以创建单点故障。

首先,我认为它可以使用内联方法。 我找到的唯一方法是:

    [MethodImpl(MethodImplOptions.AggressiveInlining)]

但它不起作用。

或者是否可以赋予方法“构造函数权限”或者不可能这样做?

最佳答案

我认为你最好的选择是重载你的构造函数然后链接它们

readonly int myReadonly;

public test(int a, int b) 
    :this (a)
{
    myReadonly = b;
}

private test(int a) 
{
    //other work
}

关于c# - 使用 "constructor rights"从构造函数调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46521156/

相关文章:

c++ - 为什么不能显式调用构造函数而析构函数可以?

C# 继承 : How to invoke the base class constructor when i call the derived class constructor

html - 导航按钮不显示内联

c# - 字符串到 byte[] - c# 的行为类似于 java

c# - 将引用(以及Unity的WWW.progress)传递给协同程序/IEnumerator

android - 如何正确构造带有继承的房间实体

c++ - 指定extern “C”时,为什么Visual Studio无法给出 undefined reference 错误?

c# - 带有嵌套对象的 JSON.NET CustomCreationConverter

c# - 获取不同的名称

c++ - 空内联函数到底发生了什么?