2017-02-10 108 views
-1

我在C#中新有這樣的變種:
如何設置初始化var c#?

var updates = await Bot.GetUpdatesAsync(offset); 


但現在要更新定義其他範圍,並用這種形狀使用它:

updates = await Bot.GetUpdatesAsync(offset); 


嘗試定義此代碼:

var updates =(string) null; 


但在這行C#編譯器收到此錯誤:

updates = await Bot.GetUpdatesAsync(offset); 
(awaitable)Task<Update[]> TelegramBotClient.GetUpdatesAsync([int offset=0],[int limit=100],[int timeout=0],[CancellationToken cancellationToken=default(CancellationToken) 
use this method recieve incoming updates using long polling. 
Usage: 
Update[] x=await GetUpdateAsync(...); 
Can not implicitly convert type 'Telegram.Bot.Types.Update[]' to 'string' 


我怎樣才能解決這個代碼感謝:

var updates =(string) null; 
+0

當然更換var updates =(string) null;如果你設置'var'爲'string'它不會轉換'Telegram.Bot.Types.Update [ ]'到'string'並且會拋出錯誤。 – Mairaj

回答

0

var updates =(Telegram.Bot.Types.Update[]) null;

+0

使用'var'關鍵字進行隱式輸入的目的是讓代碼更短,更具可讀性。但'var updates =(Telegram.Bot.Types.Update [])null;'比'Telegram.Bot.Types.Update []更新'更不可讀並且比顯式類型更長'',所以我不會推薦'var'這樣。 –

+0

同意安迪。我們可以將它聲明爲Telegram.Bot.Types.Update []更新; –

0

顯然,你的方法的返回類型是不是string,但Telegram.Bot.Types.Update[]

所以,你可以更改代碼以

Telegram.Bot.Types.Update[] updates; 
// the rest of code 
updates = await Bot.GetUpdatesAsync(offset); 
+0

感謝您的幫助我的朋友 –

+0

9分鐘後接受您的回答 –