C#學習(1)——“Hello World”創建


     創建一個簡單的“Hello World”的C#控制台應用程序

 Step 1: 文件—新建—項目

 

 Step 2:Visual C#—Windows—控制台應用程序—更改名稱為“Hello”—點擊確定

 Step 3:完成上兩步后,會生成如下的程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* using <namespace> 代表使用命名空間,之后會用到的函數就定義在命名空間中 */
namespace Hello /* 新的程序中創建了新的命名空間“Hello” */ { class Program /* 創建了類(class)“Program” */ { static void Main(string[] args) /* 主函數“Main”,以及它的參數 “args” */ { } } }

 Step 4:接下來在程序中使用命名空間System中對象Console的函數來輸出“Hello World”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
        }
    }
}

 按住control+F5,來運行程序

 

接下來嘗試用Console其他的一些函數讓輸出更豐富

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.DarkBlue;   /* 改變控制台中字的顏色 */
            Console.BackgroundColor = ConsoleColor.DarkRed;    /* 改變控制台中字的背景顏色 */
            Console.Title = "峰燁";                            /* 改變控制台標題顏色 */
            Console.WriteLine("Hello World  峰燁");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.Yellow;
            
        }
    }
}

 運行后如下:

 Step 5:改變命令行參數來改變輸出

項目—“Hello World 屬性”

 調試—命令行參數—tjufengye 你好

 更改代碼如下

namespace Hello
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.DarkBlue;   /* 改變控制台中字的顏色 */
            Console.BackgroundColor = ConsoleColor.DarkRed;    /* 改變控制台中字的背景顏色 */
            Console.Title = "峰燁";                            /* 改變控制台標題顏色 */
            Console.WriteLine(args[0] + " Hello World  峰燁 " + args[1]);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.Yellow;
        }
    }
}

運行結果如下:

 

Step 6: 調用另一個類的成員函數

首先新建一個類:

更改第二個類中的代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello
{
    class Program2
    {
        public void Cout()
        {
            Console.WriteLine("Hello World from the Program2");
        }
    }
}

 

嘗試在第一個類中調用第二個類的函數:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.DarkBlue;   /* 改變控制台中字的顏色 */
            Console.BackgroundColor = ConsoleColor.DarkRed;    /* 改變控制台中字的背景顏色 */
            Console.Title = "峰燁";                            /* 改變控制台標題顏色 */
            Console.WriteLine(args[0] + " Hello World  峰燁 " + args[1]);
            Program2 cout = new Program2();                   /* 新建一個類Program2中的對象 */
            cout.Cout();                                      /* 調用Cout函數 */
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.Yellow;
        }
    }
}

 

運行結果如下:

 

—— Author:  峰燁

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM