不一样的入门:看C# Hello World的17种写法
摘要:本文针对不同阶段、不同程度的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种写法的更多相关文章
- React入门看这篇就够了
摘要: 很多值得了解的细节. 原文:React入门看这篇就够了 作者:Random Fundebug经授权转载,版权归原作者所有. React 背景介绍 React 入门实例教程 React 起源于 ...
- [转帖]Zookeeper入门看这篇就够了
Zookeeper入门看这篇就够了 https://my.oschina.net/u/3796575/blog/1845035 Zookeeper是什么 官方文档上这么解释zookeeper,它是一个 ...
- [转]React入门看这篇就够了
摘要: 很多值得了解的细节. 原文:React入门看这篇就够了 作者:Random Fundebug经授权转载,版权归原作者所有. React 背景介绍 React 入门实例教程 React 起源于 ...
- Mybatis入门看这一篇就够了
什么是MyBatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...
- Spring入门看这一篇就够了
前言 前面已经学习了Struts2和Hibernate框架了.接下来学习的是Spring框架...本博文主要是引入Spring框架... Spring介绍 Spring诞生: 创建Spring的目的就 ...
- .NET Core实战项目之CMS 第五章 入门篇-Dapper的快速入门看这篇就够了
写在前面 上篇文章我们讲了如在在实际项目开发中使用Git来进行代码的版本控制,当然介绍的都是比较常用的功能.今天我再带着大家一起熟悉下一个ORM框架Dapper,实例代码的演示编写完成后我会通过Git ...
- Git入门看这一篇就够了! (转)
Git 的三种状态 Git 有三种状态,你的文件可能处于其中之一: 已提交(committed):数据已经安全的保存在本地数据库中. 已修改(modified):已修改表示修改了文件,但还没保存到数据 ...
- springmvc源码分析----入门看springmvc的加载过程
接上一篇我们写的入门---http://www.cnblogs.com/duanxiaojun/p/6591448.html 今天从这个门里进去我们看springmvc是如何在容器启动的时候将各个模块 ...
- Vue开发入门看这篇文章就够了
摘要: 很多值得了解的细节. 原文:Vue开发看这篇文章就够了 作者:Random Fundebug经授权转载,版权归原作者所有. 介绍 Vue 中文网 Vue github Vue.js 是一套构建 ...
随机推荐
- python发送GET或POST请求以便干一些趣事
适合级别:入门,中级 关键字 :python, http, GET, POST, 安全, 模拟, 浏览器, 验证码,图片识别, google 1 此文不是关于黑客或安全话题的! 2 使用脚本程序发 ...
- Python 调用 Shell脚本的方法
Python 调用 Shell脚本的方法 1.os模块的popen方法 通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出. > ...
- 17.出现fatal signal(SIGSEGV),code 1,fault addr 0x0 in tid 29931的问题
原因是: 在onCreate方法里面调一些东西的时候,有时候系统相关的东西还没有初始化完,你的代码就已经执行了,会出现一些问题 解决方法: 增加延时1s /一.第一步初始化,此处用的是demo的ap ...
- 安装odoo过程中出现的问题
一 centos6.5 1. simplejson error:module not found fix: easy_install simplejson 2. python version erro ...
- DEPHI XE5 XE6 ANDROID IOS开发的几点体会
DEPHI XE5 XE6 ANDROID IOS开发的几点体会 2014-09-04 20:48 1.不纠结于APK或者APP的大小.现在的客户端设备都很廉价,300元以上的新安卓设备都不在乎软件的 ...
- SQL Server WITH ROLLUP、WITH CUBE、GROUPING语句的应用
CUBE:CUBE 生成的结果集显示了所选列中值的所有组合的聚合. ROLLUP:ROLLUP 生成的结果集显示了所选列中值的某一层次结构的聚合. GROUPING:当行由 CUBE 或 ROLLUP ...
- android系统和ios系统是如何实现推送的,ios为什么没有后台推送
ios系统为什么没有后台推送? iOS 为了真正地为用户体验负责,不允许应用在后台活动.有了这个限制,但是对于终端设备,应用又是有必要“通知”到达用户的,随时与用户主动沟通起来的(典型的如聊天应用). ...
- web性能深入探究 eventloop 与浏览器渲染的时序问题 #
https://github.com/jin5354/404forest/issues/61
- webapi 版本控制
一.问题 软件开发过程中,常常会需要变更以前的接口,添加或删除接口请求字段,接口字段校验.甚至是变更请求/返回字段名称,如果强制要求所有客户端跟着一起升级代价太大.如果接口从一开始就考虑到了版本的设计 ...
- hadoop namenode HA集群搭建
hadoop集群搭建(namenode是单点的) http://www.cnblogs.com/kisf/p/7456290.html HA集群需要zk, zk搭建:http://www.cnblo ...