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

 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[]);
}
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[]);
// 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 = ; 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(, );
}
}

不一样的入门:看C# Hello World的17种写法的更多相关文章

  1. React入门看这篇就够了

    摘要: 很多值得了解的细节. 原文:React入门看这篇就够了 作者:Random Fundebug经授权转载,版权归原作者所有. React 背景介绍 React 入门实例教程 React 起源于 ...

  2. [转帖]Zookeeper入门看这篇就够了

    Zookeeper入门看这篇就够了 https://my.oschina.net/u/3796575/blog/1845035 Zookeeper是什么 官方文档上这么解释zookeeper,它是一个 ...

  3. [转]React入门看这篇就够了

    摘要: 很多值得了解的细节. 原文:React入门看这篇就够了 作者:Random Fundebug经授权转载,版权归原作者所有. React 背景介绍 React 入门实例教程 React 起源于 ...

  4. Mybatis入门看这一篇就够了

    什么是MyBatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...

  5. Spring入门看这一篇就够了

    前言 前面已经学习了Struts2和Hibernate框架了.接下来学习的是Spring框架...本博文主要是引入Spring框架... Spring介绍 Spring诞生: 创建Spring的目的就 ...

  6. .NET Core实战项目之CMS 第五章 入门篇-Dapper的快速入门看这篇就够了

    写在前面 上篇文章我们讲了如在在实际项目开发中使用Git来进行代码的版本控制,当然介绍的都是比较常用的功能.今天我再带着大家一起熟悉下一个ORM框架Dapper,实例代码的演示编写完成后我会通过Git ...

  7. Git入门看这一篇就够了! (转)

    Git 的三种状态 Git 有三种状态,你的文件可能处于其中之一: 已提交(committed):数据已经安全的保存在本地数据库中. 已修改(modified):已修改表示修改了文件,但还没保存到数据 ...

  8. springmvc源码分析----入门看springmvc的加载过程

    接上一篇我们写的入门---http://www.cnblogs.com/duanxiaojun/p/6591448.html 今天从这个门里进去我们看springmvc是如何在容器启动的时候将各个模块 ...

  9. Vue开发入门看这篇文章就够了

    摘要: 很多值得了解的细节. 原文:Vue开发看这篇文章就够了 作者:Random Fundebug经授权转载,版权归原作者所有. 介绍 Vue 中文网 Vue github Vue.js 是一套构建 ...

随机推荐

  1. java基础05 选择结构

    选择结构 public class Demo01Change { public static void main(String[] args) { /** * 实现等量的转换 */ int a = 5 ...

  2. Python并行编程(七):线程同步之事件

    1.基本概念 事件是线程之间用于通讯的对象.有的线程等待信号,有的线程发出信号.基本上事件对象都会维护一个内部变量,可以通过set方法设置为true,也可以通过clear方法设置为false.wait ...

  3. C++开源库集合

    | Main | Site Index | Download | mimetic A free/GPL C++ MIME Library mimetic is a free/GPL Email lib ...

  4. mysql参照完整性 策略设置之 on update 和 on delete

    一.当表中有外键约束的时候参照表中数据的删除和修改违背参照完整性时 可根据以下策略进行处理 1.两条策略设置为cascade的时候 参照表中的数据修改或者删除的时候改表中数据也会被删除 2.两条策略设 ...

  5. vs中nodejs代码 resharper 提示 ECMAScript2015 Feature. your Current language level is ECMAScript5的解决办法

    问题如图 错误信息:ECMAScript 2015 Feature. your Current language level is: ECMAScript5 解决方法, 打开 Resharper -& ...

  6. Java集合—Queue(转载)

    Queue用于模拟队列这种数据结构,队列通常是指“先进先出”(FIFO)的容器.新元素插入(offer)到队列的尾部,访问元素(poll)操作会返回队列头部的元素.通常,队列不允许随机访问队列中的元素 ...

  7. TensorFlow学习笔记(三)-- feed_dict 使用

    个人理解:就是TF的一种输入语法. 跟C语言的scanf(),C++的 cin>> 意思差不多,只是长相奇怪了点而已. 做完下面几个例子,基本也就适应了. 首先占位符申请空间:使用的时候, ...

  8. 简明python教程八----输入/输出

    通过创建一个file类的对象来打开一个文件,分别使用file类的read.readline或write方法来读写文件. 最后调用一个close方法来告诉Python我们完成了对文件的使用. poem= ...

  9. mysql 锁相关的视图(未整理)

    mysql 锁相关的视图 查看事务,以及事务对应的线程ID   如果发生堵塞.死锁等可以执行kill  线程ID  杀死线程      kill  199 SELECT * FROM informat ...

  10. 栈的最大值问题 max问题 min问题 队列的max问题

    常数时间求栈的最大值   问题描述: 一个栈stack,具有push和pop操作,其时间复杂度皆为O(1). 设计算法max操作,求栈中的最大值,该操作的时间复杂度也要求为O(1). 可以修改栈的存储 ...