Visual Studio 2015 IDE新功能

by vivid 3. 六月 2015 00:31

.NET Magazine國際中文電子雜誌
作 者:許薰尹
審 稿:張智凱
文章編號:N150616101
出刊日期:2015/6/3
開發工具:Visual Studio 2015 RC
版本:.NET Framework 4.6

Visual Studio 2015已經進入RC版本,想必很快就問市了。在這篇文章中,我將介紹Visual Studio 2015 IDE以及C#程式語言所提供的新功能。

IDE新功能 –Quick Action

Visual Studio 2015 IDE新的Quick Action功能,提供更佳的程式碼重構(Refactor)功能。當我們在撰寫程式碼時,常常使用using語法來引用命名空間,有時引用到了程式中根本沒有使用到的命名空間。在Visual Studio 2015 IDE工具撰寫程式碼時,沒有使用到的命名空間之using語法那行程式顏色會變淺,表示此行程式碼是多餘不必要的,若使用到某命名空間下的類別,using語法那行程式碼的顏色便會自動變回正常,請參考下圖所示:

clip_image002

圖 1:利用顏色來區分是否使用到相關命名空間。

當你將滑鼠移動到不必要的using語法上方,Visual Studio 2015會出現一個燈泡,你可以點選它,或按下「CTRL + .」組合鍵,此時就會啟動Quick Action功能,Visual Studio 2015會提示您是否進行必要的程式修訂動作,請參考下圖所示:

clip_image004

圖 2:提示是否修訂程式碼。

在Quick Action的提示視窗中您可以選取「Document」、「Project」或「Solution」三種等級的修訂動作,針對目前開啟的程式文件(Document)、專案(Project)與方案(Solution)來修訂程式碼,點選任一選項之後,便可從預覽視出來預覽結果,請參考下圖所示:

clip_image006

圖 3:預覽變更。

若選取視窗中的「Apply」按鈕便可馬上套用修訂的程式碼,參考執行結果請參考下圖所示:

clip_image008

圖 4:套用程式修訂功能。

Introduce Local功能

Introduce Local功能可以將程式抽離出來,改用一個區域變數取代。例如下圖中使用到一行DateTime.Now.ToString()程式碼以取得目前的時間,若在其它地方需要使用到時間資訊,便可這行程式碼執行的結果暫放到變數。在Visual Studio 2015中只要將此部分程式碼選取來,按「CTRL+.」啟動Quick Action,請參考下圖所示:

clip_image010

圖 5:Introduce Local功能。

選取「Introduce local for ‘DateTime.Now.ToString()’」項目,則目前的程式碼將變成如下:

class Program

{

static void Main(string[] args)

{

var v = DateTime.Now.ToString();

Console.WriteLine("Hello" + v);

}

}

Visual Studio程式碼視窗的右上方會出現一個「Rename」的小視窗,可以直接利用Rename功能改名,只要將游標停留在變數的名稱上,您可以直接輸入新的變數名稱,你也可以選取連註解或字串中的同名變數一併做改名的動作,輸完新的變數名稱之後只要按下「Apply」就可以直接套用新的變數名稱,請參考下圖所示:

clip_image012

圖 6:Rename功能。

完成的結果請參考下圖所示:

clip_image014

圖 7:Rename功能。

若變更變數名稱時,使用到的變數名稱重複了,Visual Studio也會馬上檢查命名是否衝突,並在名稱衝突時,自動顯示錯誤訊息,例如下圖,當想要將名稱命為「mystr」時,Visual Studio會將衝突的名稱利用紅色的框框起來:

clip_image016

圖 8:自動檢查命名是否衝突。

若使用到關鍵字做為變數名稱時,如輸入this,則工具會自動加上@符號,自動避免誤用關鍵字當變數名稱,請參考下圖所示:

clip_image018

圖 9:自動避免誤用關鍵字當變數名稱。

IDE新功能 – 除錯

利用Visual Studio 2015 開發程式時,在程式除錯階段,可以直接修改程式碼了,不必像過去一樣,程式出現問題時,要先停止程式執行,才能夠修改原有程式碼。例如下圖有一個簡單的主控台應用程式,我們在程式中設定了中斷點:

clip_image020

圖 10:設定中斷點。

接著按F5執行程式,遇到中斷點,Visual Studio會自動進入中斷模式,在此時您可以直接輸入程式碼,例如以下定義一個emp變數,初始化為一個Employee物件:

var emp = new Employee();

目前Employee類別的定義尚未建立,將游標停留在Employee上方按下「CTRL + .」鍵,便會提示修訂選項請參考下圖所示:

clip_image022

圖 11:修訂建議清單。

以這個例子而言,我們需要建立Employee類別的定義,選取畫面中的「Generate class for ‘Employee’ in CS6」項目後,便會自動產生Employee類別定義的程式碼,請參考下圖所示:

clip_image024

圖 12:自動產生類別定義程式碼。

IDE新功能 – 專案

在Visual Studio 2015 RC版中,大部分類型的C#專案,例如主控台應用程式(Console Application)、WPF應用程式、Windows Form應用程式、Project類型的ASP.NET Web Form、或MVC專案,都可以設定想要使用的C#程式語言版本。設定的步驟為,選取「Solution Explorer」視中的專案名稱,按滑鼠右鍵,選取在專案的「Properties」-「Build」-「Advanced」,在「Language Version」下拉式清單方塊中,選取版本,請參考下圖所示:

clip_image026

圖 13:設定C#程式語言版本。

只要按下「OK」按鈕,設定就會馬上套用到目前的專案,因此若你選取「C# 5.0」版,程式中使用到「C# 6.0」的語法時,編譯就會失敗。

Tags:

.NET Magazine國際中文電子雜誌 | Visual Studio | 許薰尹Vivid Hsu

評論 (55) -

cours de theatre paris
cours de theatre paris United States
2017/9/30 下午 10:37:46 #

This is one awesome blog post.Really thank you! Much obliged.

回覆

can ho dic phoenix
can ho dic phoenix United States
2017/10/6 下午 11:43:33 #

Really enjoyed this post.Really looking forward to read more. Want more.

回覆

Buy illegal backlinks
Buy illegal backlinks United States
2017/10/9 下午 01:38:07 #

A big thank you for your blog.Really thank you! Really Great.

回覆

kamagra sklep
kamagra sklep United States
2017/10/9 下午 03:15:45 #

I appreciate you sharing this article post. Cool.

回覆

can ho bien vung tau
can ho bien vung tau United States
2017/10/9 下午 05:32:31 #

Really enjoyed this blog article.Really thank you! Fantastic.

回覆

solarmovie
solarmovie United States
2017/10/10 下午 07:20:47 #

wow, awesome article.Much thanks again. Much obliged.

回覆

Sterling Businesses Ltd
Sterling Businesses Ltd United States
2017/10/10 下午 09:27:24 #

Thanks-a-mundo for the post.Much thanks again. Great.

回覆

pirater un compte facebook
pirater un compte facebook United States
2017/10/10 下午 11:46:05 #

Looking forward to reading more. Great article.Thanks Again. Great.

回覆

buy hacklinks
buy hacklinks United States
2017/10/12 下午 08:10:46 #

Thanks for sharing, this is a fantastic blog.Much thanks again. Want more.

回覆

dragon city hacked apk
dragon city hacked apk United States
2017/10/15 下午 02:54:06 #

Muchos Gracias for your blog.Much thanks again. Much obliged.

回覆

omega xl review
omega xl review United States
2017/10/15 下午 07:35:51 #

A big thank you for your blog article.Really looking forward to read more. Really Cool.

回覆

Osimi Tower
Osimi Tower United States
2017/10/19 上午 05:06:55 #

I appreciate you sharing this blog article.Thanks Again. Will read on...

回覆

VigRx Plus
VigRx Plus United States
2017/10/19 上午 06:48:26 #

Major thankies for the blog.Much thanks again. Want more.

回覆

you could try these out
you could try these out United States
2017/10/19 下午 05:38:26 #

I appreciate you sharing this post. Want more.

回覆

pure slim 1000 review
pure slim 1000 review United States
2017/10/20 上午 03:08:59 #

A round of applause for your article post. Cool.

回覆

vung tau melody
vung tau melody United States
2017/10/21 上午 02:52:17 #

Im grateful for the blog article.Much thanks again. Fantastic.

回覆

carte grise en ligne
carte grise en ligne United States
2017/10/21 上午 06:30:20 #

I am so grateful for your blog post.Thanks Again. Keep writing.

回覆

Turbotax phone number
Turbotax phone number United States
2017/10/24 上午 10:32:52 #

Great, thanks for sharing this blog post.Thanks Again. Want more.

回覆

Osimi Sea View
Osimi Sea View United States
2017/10/28 上午 09:53:29 #

Appreciate you sharing, great blog post.Thanks Again. Will read on...

回覆

EZ Battery Reconditioning
EZ Battery Reconditioning United States
2017/10/30 上午 08:45:50 #

I am so grateful for your article post.Much thanks again. Really Cool.

回覆

life leadership
life leadership United States
2017/11/1 上午 09:00:58 #

Hey, thanks for the article post. Keep writing.

回覆

phenocal review
phenocal review United States
2017/11/1 下午 04:27:14 #

I loved your blog article. Great.

回覆

phentaslim
phentaslim United States
2017/11/3 下午 03:35:36 #

Thanks again for the blog article.Thanks Again. Cool.

回覆

sciatica pain headaches
sciatica pain headaches United States
2017/11/15 上午 08:41:15 #

A big thank you for your blog post.Really thank you! Awesome.

回覆

avocat criminel montreal
avocat criminel montreal United States
2017/11/16 下午 07:15:40 #

Thanks for the blog article.Thanks Again. Want more.

回覆

trace name from mobile number
trace name from mobile number United States
2017/11/22 上午 03:58:10 #

Thanks-a-mundo for the article.Much thanks again. Really Cool.

回覆

swimwear
swimwear United States
2017/11/23 下午 10:46:00 #

Really appreciate you sharing this article post.Much thanks again. Cool.

回覆

Auckland Seo Company
Auckland Seo Company United States
2017/11/25 下午 07:42:23 #

Muchos Gracias for your blog post. Cool.

回覆

Chad Boonswang and Jeffrey Goodman
Chad Boonswang and Jeffrey Goodman United States
2017/11/26 下午 06:52:09 #

A round of applause for your blog.Really looking forward to read more. Fantastic.

回覆

Chad Boonswang SEO
Chad Boonswang SEO United States
2017/11/27 上午 12:59:00 #

Looking forward to reading more. Great blog post.Much thanks again. Great.

回覆

scammer melbourne
scammer melbourne United States
2017/11/29 下午 05:22:05 #

Thank you for your blog.Thanks Again. Keep writing.

回覆

Opal Skyview
Opal Skyview United States
2017/11/29 下午 11:59:47 #

I am so grateful for your blog post.Much thanks again. Keep writing.

回覆

primary trade lines
primary trade lines United States
2017/12/1 上午 01:46:32 #

Thank you ever so for you post. Really Great.

回覆

porno
porno United States
2017/12/1 下午 05:28:16 #

Very good blog article. Want more.

回覆

Initial coin offering
Initial coin offering United States
2017/12/3 上午 05:43:35 #

Very informative blog.Much thanks again.

回覆

mobile hentai
mobile hentai United States
2017/12/5 上午 10:32:05 #

A round of applause for your blog.Really looking forward to read more. Great.

回覆

Thanks a lot for the article. Keep writing.

回覆

floral law
floral law United States
2017/12/10 下午 08:07:24 #

Thanks for sharing, this is a fantastic article. Really Great.

回覆

useful reference
useful reference United States
2017/12/14 下午 05:07:10 #

Major thanks for the post.Really thank you! Really Great.

回覆

Hanukkah
Hanukkah United States
2017/12/14 下午 11:40:19 #

I really like and appreciate your article post. Keep writing.

回覆

green coffee bean for weight loss
green coffee bean for weight loss United States
2017/12/17 上午 01:07:09 #

Really appreciate you sharing this article.Really thank you! Want more.

回覆

Thanks-a-mundo for the article post.Thanks Again. Keep writing.

回覆

Enterprise
Enterprise United States
2017/12/17 下午 06:26:42 #

I really like and appreciate your post. Much obliged.

回覆

university diplomas for sale
university diplomas for sale United States
2017/12/18 上午 01:32:44 #

This is one awesome article. Keep writing.

回覆

canon drivers
canon drivers United States
2017/12/23 上午 11:30:51 #

Very good article.Much thanks again. Keep writing.

回覆

hp driver
hp driver United States
2017/12/25 下午 11:27:46 #

Really informative article post. Keep writing.

回覆

آینه
آینه United States
2017/12/26 上午 11:16:19 #

Major thankies for the blog.Really thank you!

回覆

SOCCER HIGHLIGHTS
SOCCER HIGHLIGHTS United States
2017/12/26 下午 04:00:25 #

I appreciate you sharing this article. Really Great.

回覆

hp drivers
hp drivers United States
2018/1/2 上午 11:01:46 #

Really enjoyed this blog article.Thanks Again. Really Great.

回覆

why not check here
why not check here United States
2018/1/2 下午 07:10:25 #

Looking forward to reading more. Great article post.Really thank you! Keep writing.

回覆

canon drivers
canon drivers United States
2018/1/3 上午 10:25:00 #

Very informative blog article. Want more.

回覆

online pokies for real money
online pokies for real money United States
2018/1/4 下午 09:10:41 #

I really liked your blog.Thanks Again. Awesome.

回覆

hp driver
hp driver United States
2018/1/5 下午 07:09:34 #

Major thanks for the blog article.Really thank you! Much obliged.

回覆

FBA
FBA United States
2018/1/6 上午 11:15:43 #

This is one awesome blog article.Really looking forward to read more.

回覆

新增評論




  Country flag
biuquote
  • 評論
  • 線上預覽
Loading






NET Magazine國際中文電子雜誌

NET Magazine國際中文電子版雜誌,由恆逸資訊創立於2000,自發刊日起迄今已發行超過500篇.NET相關技術文章,擁有超過40000名註冊讀者群。NET Magazine國際中文電子版雜誌希望藉於電子雜誌與NET Developer達到共同學習與技術新知分享,歡迎每一位對.NET 技術有興趣的朋友們多多支持本雜誌,讓作者群們可以有持續性的動力繼續爬文。<請加入免費訂閱>

月分類Month List