首页 > Windows开发 > 详细

C#学习(1)——“Hello World”创建

时间:2015-03-16 19:11:07      阅读:366      评论:0      收藏:0      [点我收藏+]

     创建一个简单的“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:  峰烨

 

C#学习(1)——“Hello World”创建

原文:http://www.cnblogs.com/tjufengye/p/4341457.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!