C#学习进阶Hello World的17种写法代码分享
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);
}
}
上一篇: Java线程的生命周期和状态控制_动力节点Java学院整理
下一篇: 巧用CSS滤镜做图案文字