欢迎光临
我们一直在努力

17种"Hello World"!-.NET教程,C#语言

建站超值云服务器,限时71元/月

使用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);

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 17种"Hello World"!-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章