首页 > 编程语言 >转载:不一样的入门:看C# Hello World的17种写法

转载:不一样的入门:看C# Hello World的17种写法

时间:2022-11-17 13:00:28浏览次数:65  
标签:17 C# void HelloWorld class static using World public

https://www.cnblogs.com/jara/p/3456672.html

 

摘要:本文针对不同阶段、不同程度的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);
    }
}
复制代码

 


个人主要研究:金融系统、MIS系统、人力资源管理系统、数据采集系统、权限管理系统等等系统。主攻C#开发语言,Oracle、Sql Server,WCF和Remoting通信。
如需联系可加QQ:442389681 Email:[email protected] 手机:18922735098
QQ群交流:186841119 (请注明来自博客园)
博客园地址:http://www.cnblogs.com/jara/       http://www.cnblogs.com/luoyuhao/
提示:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果觉得还有帮助的话,可以点一下右下角的【推荐】,希望能够持续的为大家带来好的技术文章!想跟我一起进步么?那就【关注】我吧。
如果对文章有任何问题,都可以在评论中留言,我会尽可能的答复您,谢谢您的阅读

标签:17,C#,void,HelloWorld,class,static,using,World,public
From: https://www.cnblogs.com/OURS2025/p/16899128.html

相关文章

  • 华三防火墙主备ACL
    discur|inpolicy查看policy-based-route有线网络permitnode5policy-based-route有线网络permitnode10ippolicy-based-route有线网络natglobal-policy......
  • React-App环境搭建和项目启动
    node环境>=14.0.0npm环境>=5.6安装create-react-app:npmi-gcreate-react-app(cnpm/yarn)创建项目:create-react-appreact-project(项目名称)进入项目根目录:cdreact-p......
  • 2022CCPC桂林 (2022 China Collegiate Programming Contest (CCPC) Guilin Site)
    链接:https://codeforces.com/gym/104008A.LilyC++Code#include"bits/stdc++.h"usingnamespacestd;usingi64=longlong;voidsolve(){intn;cin......
  • 11.django-csrftoken
    django为用户实现防止跨站请求伪造的功能,通过中间件django.middleware.csrf.CsrfViewMiddleware来完成。而对于django中设置防跨站请求伪造功能有分为全局和局部。CSRF(C......
  • Msb网络安全-CSS
    一、CSS基础概念CSS有两个重要的概念,分别是样式和布局CSS的样式分为两种:一种是文字的样式,一种是盒模型的样式CSS的辅助页面布局:完成HTML不能完成的功能,比如并排显示,比如......
  • 10.django-cookie&session
    我们知道HTTP协议是无状态协议,也就是说每个请求都是独立的!无法记录前一次请求的状态。但HTTP协议中可以使用Cookie来完成会话跟踪!在Web开发中,使用session来完成会话跟踪,ses......
  • TCP协议中的三次握手和四次挥手
    理解:窗口和滑动窗口TCP的流量控制TCP使用窗口机制进行流量控制什么是窗口?连接建立时,各端分配一块缓冲区用来存储接收的数据,并将缓冲区的尺寸发送给另一端接收方发送的确认信......
  • Redis 和 memache 缓存的区别
    1.数据类型 Redis数据类型丰富,支持setlisthash等类型 memcache支持简单数据类型,需要客户端自己处理复杂对象 2.持久性 redis支持数据落地持久化存储,并不是所有的数据......
  • eCos疑问——两个cyg_user_start函数
    mingdu.zheng<at>gmail<dot>com 两个cyg_user_start函数在阅读eCos代码的过程中发现整个系统定义了两个cyg_user_start函数,一个位于packages/infra/<version>/src/......
  • cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 ‘aop:aspectj-autopro
    Causedby:org.xml.sax.SAXParseException;lineNumber:12;columnNumber:28;cvc-complex-type.2.4.c:通配符的匹配很全面,但无法找到元素‘aop:aspectj-autoproxy’......