不一样的入门:看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 是一套构建 ...
随机推荐
- MyBatis 工作流程及插件开发
1. MyBatis 框架分层架构 2. MyBatis 工作流程 获取 SqlSessionFactory 对象: 解析配置文件(全局映射,Sql映射文件)的每一个信息,并保存在Configurat ...
- (4.20)SQL Server数据库启动过程,以及启动不起来的各种问题的分析及解决技巧
转自:指尖流淌 https://www.cnblogs.com/zhijianliutang/p/4085546.html SQL Server数据库启动过程,以及启动不起来的各种问题的分析及解决技巧 ...
- 使用maven为web工程引入jstl包时报错了
原pom文件: <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</a ...
- MySQL基础命令
目录: MySQL概述 安装及启动登陆mysql操作 MySQL基本数据类型 MySQL数据库基本SQL操作 MySQL高级SQL操作 一.MySQL概述 1.什么是数据库? 数据库(Database ...
- OOM问题分析
一.背景 在实际的开发中,性能问题的分析一直是运维团队的痛点,无论是缓慢内存溢出还是迅速的内存爆炸,对系统和业务的破坏都是快速而巨大的,此贴分享下简单的分析内存问题的经验. 二.相关名词 分代:根据对 ...
- android 带CheckBox对话框
package com.example.dialog4; import android.os.Bundle;import android.app.Activity;import android.app ...
- 189. Rotate Array(两次反转)
DescriptionHintsSubmissionsDiscussSolution Pick One Rotate an array of n elements to the right by ...
- web前端基础——初识JavaScript
1 JavaScript概述 JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚 ...
- webapi 版本控制
一.问题 软件开发过程中,常常会需要变更以前的接口,添加或删除接口请求字段,接口字段校验.甚至是变更请求/返回字段名称,如果强制要求所有客户端跟着一起升级代价太大.如果接口从一开始就考虑到了版本的设计 ...
- Promise原理剖析
传统的异步回调编程最大的缺陷是:回调地狱,由于业务逻辑非常复杂,代码串行请求好几层:并行请求以前也要通过引用step.async库实现.现在ES6推出了Promise,通过Promise的链式调用可以 ...