【C#】考你一个输出“Hello World”程序
前天同学问我这样一个面试题:完成如下代码,使其输出“Hello World!”
if() printf("Hello");我很快想到了答案。然后同学告诉我这个题不止一个答案。
else printf(" World!");
如果换成C#,也可以这样问:完成如下代码,使其输出“Hello World!”
using System;用C#语言,至少我已经想到了三种不同原理的答案。
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
if (/* 补充这里 */)
Console.Write("Hello");
else
Console.Write(" World!");
}
}
}
欢迎大家想出更多的答案。
我会在若干天以后公布我所想到的答案。
[解决办法]
using System;
public class HelloWorld
{
public HelloWorld()
{
Console.WriteLine("HELLO WORLD");
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
}
}
using System;
public class HelloWorld
{
public void helloWorld()
{
Console.WriteLine("HELLO WORLD");
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.HelloWorld();
}
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();
}
}
}
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
if (Convet.ToBool(Console.Write("Hello")))
Console.Write("Hello");
else
Console.Write(" World!");
}
}
}
[解决办法]
学习
[解决办法]
洗耳恭听
[解决办法]
2楼答案不行的,首先改为这样吧:Convert.ToBoolean (Console.Write("Hello")) 。然后还是
错误 参数“1”: 无法从“void”转换为“object”,继续关注
[解决办法]
学习一下
[解决办法]
new Func<bool>(() => { Console.Write("Hello"); return false; }).Invoke()
没看懂!!!!
[解决办法]
那是。呵呵。有scanf么。
[解决办法]
匿名方法,和下面的一样的..
public bool Function()
{
Console.Write("Hello");
return false;
}
if(Function()){ ..
[解决办法]
Hello应该要在if里面打印,想一下先
[解决办法]
还真想不出,还有什么样的办法呢,思路与上面完全不一样的,期待!
[解决办法]
我来一个,LINQ
static void Main(string[] args)
{
if ((args = new string[1]{"Hello"}).Length > 0 &&
args.Any(s=>{Console.Write(s);return false;}))
{
Console.Write("Hello");
}
else
{
Console.Write("World");
}
}
[解决办法]
呵呵 ~~~
[解决办法]
还真不会。。。。。。
[解决办法]
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
if (a())
Console.Write("Hello");
else
Console.Write(" World!");
Console.ReadLine();
}
static bool a()
{
Console.Write("Hello");
return false;
}
}
}
[解决办法]
if (Console.Write("Hello") is object)
Console.Write("Hello");
else
Console.Write(" World!");
[解决办法]
想不出来......
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 0 ? true : ((Func<bool>)delegate { Main(new string[] { "a" }); return false; }).Invoke())
Console.Write("Hello");
else
Console.Write(" World!");
}
}
}
[解决办法]
真想不出,还有什么样的办法呢,思路与上面完全不一样的,期待
[解决办法]
综合20#,22#
static void Main(string[] args)
{
if (args.Length == 0 ? (Main(args = new string[]{string.Empty}) is object) : true)
{
Console.Write("Hello");
}
else
{
Console.Write("World");
}
}
[解决办法]
//发散一下。。。
class Program
{
class BrotherLi
{
public static bool IsGirl
{
get
{
Console.Write("Hello");
return false;
}
set { Console.Write(" World"); }
}
}
static void Main()
{
BrotherLi.IsGirl = !BrotherLi.IsGirl;
Console.ReadKey();
}
}
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
if (true)Console.Write("Hello World!");else if (false)
Console.Write("Hello");
else
Console.Write(" World!");
}
}
}
[解决办法]

[解决办法]
多是牛人,学习了
[解决办法]
代码注入
