小编use*_*189的帖子

在nullable int和int之间转换

我想做这样的事情:

int? l = lc.HasValue ? (int)lc.Value : null; 
Run Code Online (Sandbox Code Playgroud)

其中lc是可以为空的枚举类型,比如EMyEnumeration?.所以我想测试lc是否有值,所以如果然后将其int值赋给l,否则l为null.但是当我这样做时,C#抱怨'无法确定条件表达式的错误类型,因为'int'和''之间没有隐式转换.

我怎样才能使它正确?提前致谢!!

c# int enumeration nullable

1
推荐指数
2
解决办法
1292
查看次数

如何设置一次属性并且不能更改?

我有一个Message类,它具有三个属性Content,Type和UniqueId。创建Message对象时,Content和Type是已知的,因此我可以将它们传递给类的构造函数,并使Content和Type属性为只读,以便不再更改它们的值。但是,对于UniqueId,我需要在创建对象后在代码中对其进行计算,并将其值赋予UniqueId属性。因为我无法将UniqueId传递给构造函数并使该属性为只读,所以我想知道是否有这样一种方法,一旦设置了属性UniqueId,就无法再更改其值了?

public class Message
{
    private readonly string content;
    private readonly AuditMessageType type;
    private Guid messageUId;

    public Message(string syslogMessage, AuditMessageType messageType, Guid messageUniqueId = new Guid())
    {
        content = syslogMessage;
        type = messageType;
        messageUId = messageUniqueId;
    }

    public string Message
    {
        get { return message; }
    }

    public AuditMessageType Type
    {
        get { return type; }
    }

    public Guid MesageUniqueId
    {
        get { return messageUId; }
        set { messageUId = value; } // How to make UniqueId property set once …
Run Code Online (Sandbox Code Playgroud)

c# constructor properties

1
推荐指数
1
解决办法
2181
查看次数

标签 统计

c# ×2

constructor ×1

enumeration ×1

int ×1

nullable ×1

properties ×1