不一样的入门:看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 是一套构建 ...
随机推荐
- html禁止选中文字
简单的办法,可以直接使用CSS: div { -moz-user-select:none; -webkit-user-select:none; user-select:none; } 嗯,就酱~
- Mysql2索引
索引分类: 作用:优化查询,select查询有三种情况:缓存查询(不在mysql中进行数据查询),全表查询,索引扫描 Btree(btree b+tree b*tree) Rtree HASH Ful ...
- Linux ls命令
ls:即列表List的意思,用来列出目录下的文件用来列出给定目录下的文件,参数为空默认列出当前目录下的文件. 用法是:ls [选项] [目录] 常用的选项有 -a, –all 列出目录下的所有文件,包 ...
- Android ListView工作原理完全解析(转自 郭霖老师博客)
原文地址:http://blog.csdn.net/guolin_blog/article/details/44996879 在Android所有常用的原生控件当中,用法最复杂的应该就是ListVie ...
- 关于android:windowNoTitle不起作用的解决办法
今天测试一个新功能的时候,在styles.xml设置<item name="android:windowNoTitle">true</item>并没有生效, ...
- 2.AS入门教程
AndroidStudio 本文是关于androidStudio的一些基础知识 介绍 Google官方的Android集成开发环境(IDE = Integrated Development Envir ...
- JavaScript Big-Int
这个库是为JavaScript中的大整数操作,如加,减,乘,除,mod,比较等. 这个库的原理是模拟笔和纸的操作,你可以操作整数,大到你的RAM允许. 例 var bigInt = require(' ...
- Push h.264 rawdata to rtmp server
Push h.264 rawdata to rtmp server /* The MIT License (MIT) Copyright (c) 2013-2015 SRS(ossrs) Permis ...
- MongoDB的固定集合
一.MongoDB固定集合概念 固定集合指的是事先创建,并且大小固定的集合.即假设一个集合设置了固定大小为100,再添加一条文档的时候,会把最前面的文档剔除,永远只保留100条数据. 固定集合特性:固 ...
- 20165324 实验二《Java面向对象程序设计》实验报告
20165324 实验二<Java面向对象程序设计>实验报告 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:何春江 学号:20165324 指导教师:娄嘉鹏 实验日期: ...