![](/img/trans.png)
[英]How to assign different values of variables to different instances of the same object?
[英]How to assign a same value to different properties using Object Initializer
我嘗試將值分配給對象初始化程序中的兩個不同屬性,但失敗。
在下面的代碼中,我嘗試將Expand和Select屬性分配為true。 但是我得到了錯誤“ 名稱Select在當前上下文中不存在 ”
這是我的代碼
public class MyClass{
public String Title{get;set;}
public String Key{get;set;}
public bool Expand{get;set;}
public bool Select{get;set;}
public bool Editable{get;set;}
}
new MyClass()
{
Title = "Murali",
Key = "MM",
Expand = Select = true
}
我還需要基於這兩個屬性分配另一個屬性Editable
就像是
new MyClass()
{
Ediatable=(Select && Expand)
}
我該如何做以上邏輯? 對象初始化器有支持嗎?
你可以不是指你在的右側構造對象的屬性=
,也就是說,你只能分配給屬性,但他們從沒有看過。
可能的解決方案:
var expandAndSelect = true;
var result = new MyClass
{
Title = "Murali",
Key = "MM",
Expand = expandAndSelect,
Select = expandAndSelect,
};
和
var select = true;
var expand = false;
var result = new MyClass
{
Expand = expand,
Select = select,
Editable = select & expand,
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.