首页 > 开发 > 综合 > 正文

17种Hello World!

2024-07-21 02:25:40
字体:
来源:转载
供稿:网友


使用c#编写不同的“hello world”程序

1. a beginners hello world
public class helloworld
{
public static void main()
{
system.console.writeline("hello world");
}
}
2. slightly improved version
using system;

public class helloworld
{
public static void main()
{
console.writeline("hello world");
}
}
3. command line arguments
using system;

public class helloworld
{
public static void main(string[] args)
{
console.writeline(args[0]);
}
}
4. from constructor
using system;
public class helloworld
{
public helloworld()
{
console.writeline("hello world");
}

public static void main()
{
helloworld hw = new helloworld();
}
}
5. more oo
using system;
public class helloworld
{
public void helloworld()
{
console.writeline("hello world");
}

public static void main()
{
helloworld hw = new helloworld();
hw.helloworld();
}
}
6. from another class
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. inheritance
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. static constructor
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. exception handling
using system;

public class helloworld
{
public static void main(string[] args)
{
try
{
console.writeline(args[0]);
}
catch(indexoutofrangeexception e)
{
console.writeline(e.tostring());
}
}
}
10. creating a dll and using it in an application
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 property
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 delegates
using system;
class helloworld
{
static void writehelloworld() {
console.writeline("helloworld");
}
static void main() {
simpledelegate d = new simpledelegate(writehelloworld);
d();
}
}
13. using attributes
#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 interfaces
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();
}
}
15. dynamic 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);

// invoking a method

object[] nmthdparams = new object[] {};
string strhelloworld = (string) hw.invokemember(
"writehelloworld", bindingflags.default |
bindingflags.invokemethod, null,
nobj, nmthdparams);

console.writeline(strhelloworld);
}
}
}
16. unsafe 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. using 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);
}
}

商业源码热门下载www.html.org.cn

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表