C#学习进阶Hello World的17种写法代码分享

 更新时间:2013年12月04日 09:17:10   作者:  
本文针对不同阶段、不同程度的C#学习者,介绍了C# Hello World的17种不同写法,C# Hello World写法入门、C# Hello World写法进阶、C# Hello World的特别写法三种角度进行推进

C# Hello World写法入门:

1. 初学者

复制代码 代码如下:

public class HelloWorld
{
    public static void Main()
    {
        System.Console.WriteLine("HELLO WORLD");
    }
}

2. 改进的HELLO WORLD

复制代码 代码如下:

using System;

public class HelloWorld
{
    public static void Main()
    {
        Console.WriteLine("HELLO WORLD");
    }
}

 3. 命令行形式

 

复制代码 代码如下:

 using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine(args[0]);
    }
}
 

 4. 构造函数

 

复制代码 代码如下:

 using System;

public class HelloWorld
{
    public HelloWorld()
    {
        Console.WriteLine("HELLO WORLD");
    }

    public static void Main()
    {
        HelloWorld hw = new HelloWorld();
    }
}
 


 C# Hello World写法进阶:

 5. 面向对象

复制代码 代码如下:

using System;

public class HelloWorld
{
    public void helloWorld()
    {
        Console.WriteLine("HELLO WORLD");
    }

    public static void Main()
    {
        HelloWorld hw = new HelloWorld();
        hw.HelloWorld();
    }
}

6. 从其他类

复制代码 代码如下:

using System;
public class HelloWorld
{
    public static void Main()
    {
        HelloWorldHelperClass hwh = new HelloWorldHelperClass();
        hwh.writeHelloWorld();
    }
}

public class HelloWorldHelperClass
{
    public void writeHelloWorld()
    {
        Console.WriteLine("Hello World");
    }
}

7. 继承

复制代码 代码如下:

abstract class HelloWorldBase
{
    public abstract void writeHelloWorld();
}
class HelloWorld : HelloWorldBase
{
    public override void writeHelloWorld()
    {
        Console.WriteLine("Hello World");
    }
}
class HelloWorldImp
{
    static void Main()
    {
        HelloWorldBase hwb = HelloWorld;
        HelloWorldBase.writeHelloWorld();
    }
}

8. 静态构造函数

复制代码 代码如下:

using System;
public class HelloWorld
{
    private static string strHelloWorld;

    static HelloWorld()
    {
        strHelloWorld = "Hello World";
    }
    void writeHelloWorld()
    {
        Console.WriteLine(strHelloWorld);
    }

    public static void Main()
    {
        HelloWorld hw = new HelloWorld();
        hw.writeHelloWorld();
    }
}

9. 异常处理

复制代码 代码如下:

using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        try
        {
            Console.WriteLine(args[0]);
        }
        catch (IndexOutOfRangeException e)
        {
            Console.WriteLine(e.ToString());
        }
    }
}

10. 名字空间

复制代码 代码如下:

using System;

namespace HelloLibrary
{
    public class HelloMessage
    {
        public string Message
        {
            get
            {
                return "Hello, World!!!";
            }
        }
    }
}
//------  
using System;  
using HelloLibrary;

namespace HelloApplication
{
    class HelloApp
    {

        public static void Main(string[] args)
        {
            HelloMessage m = new HelloMessage();
        }
    }
}

11. 属性

复制代码 代码如下:

using System;
public class HelloWorld
{
    public string strHelloWorld
    {
        get
        {
            return "Hello World";
        }
    }

    public static void Main()
    {
        HelloWorld hw = new HelloWorld();
        Console.WriteLine(cs.strHelloWorld);
    }
}

12. 代理

复制代码 代码如下:

using System;
class HelloWorld
{
    static void writeHelloWorld()
    {
        Console.WriteLine("HelloWorld");
    }
    static void Main()
    {
        SimpleDelegate d = new SimpleDelegate(writeHelloWorld);
        d();
    }
}


13. 使用属性

复制代码 代码如下:

#define DEBUGGING

using System;
using System.Diagnostics;

public class HelloWorld : Attribute
{
    [Conditional("DEBUGGING")]
    public void writeHelloWorld()
    {
        Console.WriteLine("Hello World");
    }

    public static void Main()
    {
        HelloWorld hw = new HelloWorld();
        hw.writeHelloWorld();
    }
}

14. 接口

复制代码 代码如下:

using System;

interface IHelloWorld
{
    void writeHelloWorld();
}

public class HelloWorld : IHelloWorld
{
    public void writeHelloWorld()
    {
        Console.WriteLine("Hello World");
    }

    public static void Main()
    {
        HelloWorld hw = new HelloWorld();

        hw.writeHelloWorld();
    }
}

C# Hello World的特别写法:

15. 动态Hello World

复制代码 代码如下:

using System;  
using System.Reflection;

namespace HelloWorldNS
{
    public class HelloWorld
    {
        public string writeHelloWorld()
        {
            return "HelloWorld";
        }

        public static void Main(string[] args)
        {
            Type hw = Type.GetType(args[0]);
            // Instantiating a class dynamically 
            object[] nctorParams = new object[] { };
            object nobj = Activator.CreateInstance(hw, nctorParams);//, nctorParams); 
            // Invoking a method 
            object[] nmthdParams = new object[] { };
            string strHelloWorld = (string)hw.InvokeMember("writeHelloWorld", BindingFlags.Default | BindingFlags.InvokeMethod, null, nobj, nmthdParams);
            Console.WriteLine(strHelloWorld);
        }
    }
}

16. 不安全代码Hello World

复制代码 代码如下:

using System;

public class HelloWorld
{
    unsafe public void writeHelloWorld(char[] chrArray)
    {
        fixed (char* parr = chrArray)
        {
            char* pch = parr;
            for (int i = 0; i < chrArray.Length; i++)
                Console.Write(*(pch + i));
        }
    }

    public static void Main()
    {
        HelloWorld hw = new HelloWorld();
        char[] chrHelloWorld = new char[] { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' };
        hw.writeHelloWorld(chrHelloWorld);
    }
}

17. 使用InteropServices

复制代码 代码如下:

using System;
using System.Runtime.InteropServices;

class Class1
{
    [DllImport("kernel32")]
    private static extern int Beep(int dwFreq, int dwDuration);

    static void Main(string[] args)
    {
        Console.WriteLine("Hello World");
        Beep(1000, 2000);
    }
}

相关文章

  • Unity Shader实现素描风格的渲染

    Unity Shader实现素描风格的渲染

    这篇文章主要为大家详细介绍了Unity Shader实现素描风格的渲染,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • C#设计模式之外观模式介绍

    C#设计模式之外观模式介绍

    外观模式:为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层的接口,这个借口使得这子系统容易使用
    2012-10-10
  • c#操作Redis的5种基本类型汇总

    c#操作Redis的5种基本类型汇总

    这篇文章主要给大家介绍了关于c#操作Redis的5种基本类型,文中通过示例代码介绍的非常详细,对大家的学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-07-07
  • C#实现注册码注册机制效果详解

    C#实现注册码注册机制效果详解

    这篇文章主要为大家详细介绍了C#如何实现注册码注册机制效果,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
    2023-01-01
  • c# 代码调试技巧和如何远程调试

    c# 代码调试技巧和如何远程调试

    这篇文章主要介绍了c# 代码调试技巧和如何远程调试,帮助大家更好的理解和使用c#编程语言,感兴趣的朋友可以了解下
    2020-11-11
  • C#日期转换函数分享

    C#日期转换函数分享

    这篇文章介绍了C#日期转换函数,有需要的朋友可以参考一下
    2013-10-10
  • c#模拟平抛运动动画的方法详解

    c#模拟平抛运动动画的方法详解

    本篇文章是对使用c#模拟平抛运动动画的方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C#默认双缓冲技术实例分析

    C#默认双缓冲技术实例分析

    这篇文章主要介绍了C#默认双缓冲技术,实例分析了双缓冲技术的设置与实现技巧,需要的朋友可以参考下
    2015-06-06
  • C#连接MySQL的两个简单代码示例

    C#连接MySQL的两个简单代码示例

    这篇文章主要介绍了C#连接MySQL的简单代码示例,需要的朋友可以参考下
    2017-06-06
  • c# Process.Start()找不到系统文件的解决方法

    c# Process.Start()找不到系统文件的解决方法

    vs1027在X64应用程序下执行process.start()时,OK;但是在X86应用程序下执行process.start(),报错:找不到系统文件,本文就详细的介绍一下解决方法,感兴趣的可以了解一下
    2023-09-09

最新评论