Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

I recently saw some questions on SO that asked if you could make a Hello World program without using any semi colons using C#. The challenge is to do this again, but in the shortest amount of code possible!

share|improve this question
1  
I assume this is meant to be a language-specific challenge? Otherwise there are plenty of languages where this task would be trivial. (For example, 'Hello World' is a valid GolfScript program that prints Hello World. Although HQ9+ will easily beat it.) – Ilmari Karonen Apr 17 '12 at 14:51
@llmari Sorry, yes, I have updated question. I would have tagged it C# but it doesn't exist and I don't have the rep to create it, thanks. – Bali C Apr 17 '12 at 15:41

8 Answers

up vote 5 down vote accepted

C# (114)

class M{static void Main(){if(typeof(System.Console).GetMethods()[78].Invoke(null,new[]{"Hello, world!"})is M){}}}

Note that the proper index for Write(string)/WriteLine(string) may be different on your system. However, since there are only 106 methods total, I'm almost certain either Write(string) or WriteLine(string) will be a two-digit index number on every system, so the character count should be generally valid.

Demo: http://ideone.com/5npky (Write method is apparently index 23 here)

share|improve this answer
This solution is brilliant. +1 – primo Apr 22 '12 at 10:40
I also thought is was ironic that after putting it together, it came out exactly 1 character shorter than yours :) – mellamokb Apr 23 '12 at 17:25

C, 33 chars

main(){if(puts("Hello World")){}}
share|improve this answer

C# 85 chars
class H{static void Main(){if(System.Console.Out.WriteAsync("Hello, world!")is H){}}}

share|improve this answer
Which .NET version is this? .NET 4 doesn't seem to contain a definition for TextWriter.WriteAsync(). – primo Apr 26 '12 at 6:45
2  
@primo .NET 4.5 does. – Bob Apr 26 '12 at 10:58

115 Bytes

class H{static void Main(){if(((System.Action)(()=>System.Console.Write("Hello, world!"))).DynamicInvoke()is H){}}}

It's likely possible to produce something a bit shorter, but I'm pretty sure that you're going to need make some sort of asynchronous call.

share|improve this answer

C, 36 35 bytes

main(int v[puts("Hello World")]){}

Well, it's not C#, posting just for fun..

:-)

edit: changed from char* to int, as suggested by l0n3_shArk

share|improve this answer
2  
Close enough :) +1 – Bali C Apr 18 '12 at 14:26
1  
changing char* to int reduces the size by 1 :) – l0n3_sh4rk Apr 19 '12 at 9:30

Java (96 93 chars)

Not C#, but I figure Java is similar enough:

class H{public static void main(String[]a){if(System.out.append("Hello world").equals(a)){}}}

Thanks to Prince John Wesley, here is a shorter version of 89 chars:

class H{public static void main(String[]a){if(System.out.printf("Hello world")==null){}}}
share|improve this answer
How about class H{static void main... Not sure if that trick applies to Java as well :) – mellamokb Apr 23 '12 at 20:18
Nope, that's not allowed: main should be public, or at least my compiler tells me so. Strangely enough, the class doesn't need to be. – AardvarkSoup Apr 23 '12 at 20:33
You can actually make it shorter by using a static block if you ignore the output to stderr: class H{static{if(System.out.append("Hello world").equals(0)){}}} :) – mellamokb Apr 24 '12 at 3:15
@AardvarkSoup: Use System.out.append("Hello World")!=null – Prince John Wesley Apr 30 '12 at 10:30
@AardvarkSoup: printf or format methods will do the same. – Prince John Wesley Apr 30 '12 at 10:32
show 3 more comments

96 95 94 chars

A bit of a cheat, but works if you have IronRuby installed:

class P{static void Main(){if(IronRuby.Ruby.CreateEngine().Execute("puts'Hello World'")>1){}}}
share|improve this answer
2  
You can save 1 char by replacing == with a one-character comparison operator. – Peter Taylor Apr 19 '12 at 9:42
@PeterTaylor I've updated the code. Thanks for this great suggestion! – w0lf Apr 19 '12 at 9:47
I also removed the space between puts and the string. puts'Hello World' is valid ruby code – w0lf Apr 19 '12 at 9:49

In C#. 95 chars

class X{static void Main(){if(((Action<String>)Console.Write).Invoke("Hello, World!")is X){}}}
share|improve this answer
Except you have to qualify all the types with System.Action, System.String, System.Console.Write since there are no libraries imported. Adding those back in makes your code 115 characters, which currently is a tie with the longest C# solution: ideone.com/4f8EU – mellamokb Apr 25 '12 at 22:12
Not to mention it doesn't actually output anything, even with the corrections. This however does, also 115B: ideone.com/TeGz1 – primo Apr 25 '12 at 23:31
@primo: Actually the strange thing is, when I run the ideone code I posted in LINQPad it works fine, and prints out the text. It's only on ideone that it doesn't. Wonder if it's some sort of mono or ideone limitation. – mellamokb Apr 26 '12 at 13:21
Works in VS 2010 though... – SamuelWarren Apr 26 '12 at 13:25
You're code above works in VS 2010 without any import statements? How? – mellamokb May 21 '12 at 16:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.