是否可以在方法(現有類型string,int,double或你自己的類型)的參數中定義變量的類型和值?如何在c#中定義參數方法中變量的類型和值?
原因是僅在參數沒有超出函數的地方定義變量,就像其他變量一樣。
只是爲了測試實施例:
public string test(string x){ return x; }
test(new StringBuilder{"New created string!!!"}[0].ToString()));
以上將只返回第一個字符,我們想返回孔字符串(或任何其它類型的)。
如果我們嘗試使用的測試方法,我們會使用這樣的:
1. example
string x = "some string";
test(x);
or
2. example
test("some string");
could we make something to write variable inside parameter of method, something like
3. example (not valid)
test(new string("some string "));
的一點是,你把變量未事先定義它像在1或2個例子
你的意思是[*泛型*](http://msdn.microsoft.com/en-us/library/512aeb7t.aspx)?你能舉一個例子說明你有什麼和你想要**嗎? – Sinatr
這真的不清楚你的意思,但如果你不想只是第一個字符,刪除'[0]'部分... –
@Sinatr很好,不是泛型,嘗試在測試方法中插入新的變量參數x沒有放置「」,或者先定義變量。 – tonni