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:  峰烨

原文地址:https://www.cnblogs.com/tjufengye/p/4341457.html