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;
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]);
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);