![](/img/trans.png)
[英]c# tell Visual Studio that property is not null after method call
[英]Call property after method in c#
方法之后可以調用屬性嗎?
例:
public static class Test
{
public static string Show { get; set; }
public static void Get(string s)
{
Show = s;
}
}
並這樣調用:
Test.Get("HI").Show;
更新2:流暢的界面
如果您要實現流利的界面,建議您這樣重命名方法:
public class Test
{
public string Show { get; set; }
public Test ConfigureShow(string show)
{
Show = show;
return this;
}
}
您的方法現在更具可讀性,並且清楚地定義了方法的意圖:
var test = new Test()
.ConfigureShow("HI");
使類中的其余方法遵循相同的模式,並將您的方法鏈接在新行上,以提高可讀性。
更新1:您的意圖是什么?
您嘗試這樣做有什么特殊原因嗎? 您的課程有幾個問題:
Get
的方法,該方法接受一個參數然后修改該對象。 通常, Get
方法意味着您只是從對象中獲取某些東西。 出什么問題了:
var show = "HI";
var test = new Test();
test.Show = show;
//You already have your text in the variable show, so why do you want to access it again?
OP問題的原始答案
public class Test
{
public string Show { get; set; }
public Test Get(string s)
{
Show = s;
return this;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.