C#學習第一彈之Hello World

來源:互聯網
上載者:User

標籤:

這學期開始了C#的學習,俗話說的好,“萬事”從Hello World開始,先貼上自己的Hello World代碼。

1 class HelloWorld2 {3     static void Main( string[] args )4     {5         System.Console.WriteLine("Hello World");6     }7 }

以上便是一個最基本的Hello World程式,它代表了一個C#程式的基本架構。任何一個C#程式都必須至少含有一個類,且程式從Main函數開始執行,所以很顯然Main函數必須是一個靜態方法。其中,System為命名空間,概念類似於C++中的namespace,並且System是一個最上層的namespace,它裡面還嵌套了許多namespace。而Console是類名,WriteLine是一個靜態方法。

注意:不同於C++和Java,這裡Main函數首字母要大寫,因為C#推薦變數名用駝峰命名法,如上面的args,類、函數、命名空間等用帕斯卡命名法,如HelloWorld,WriteLine,System。

C#中提供了using語句,可以為編程帶來方便,所以也可以寫成如下代碼:

1 using System;2 3 class HelloWorld4 {5     static void Main( string[] args )6     {7         Console.WriteLine("Hello World");8     }9 }

也可以用上命名空間,並且更改一下顏色。

 1 using System; 2  3 namespace HelloWorld 4 { 5     class MyHelloWorld 6     { 7         static void Main( string[] args ) 8         { 9             Console.Title = "hxy‘s first program";10             Console.BackgroundColor = ConsoleColor.White;11             Console.ForegroundColor = ConsoleColor.DarkGreen;12             Console.WriteLine("Hello World");13         }14     }15 }

 

 亦或是彈出一個對話方塊。

 1 namespace FirstMessageBox 2 { 3     class HelloWorld 4     { 5         static void Main( string[] args ) 6         { 7             System.Windows.Forms.MessageBox.Show("Hello World"); 8         } 9     }10 }

這裡需要添加上System.Windows.Forms的引用。

 

C#學習第一彈之Hello World

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.