[C#版本]有趣啊!各種編程語言實現 2 + 2 = 5


首頁新聞里面那個

[新聞頭條]有趣啊!各種編程語言實現 2 + 2 = 5

其他的就不評論的,但是其中C#版的 真是逗比啊...評論中各種吐槽有木有...

static void Main (string[] args)
{
    var x = 2;
    var y = 2;

    if (1 == 0) ;
    {
        ++x;
    }

    Console.WriteLine (x + y);
}

 

所以我閑來無聊 ~~ 也來實現一個....

static void Main(string[] args)
{
    using (var p = new CSharpCodeProvider())
    {
        Console.SetOut((TextWriter)p.CompileAssemblyFromSource(new CompilerParameters() { GenerateExecutable = false, GenerateInMemory = true, IncludeDebugInformation = false }, "using System.IO;using System.Text;class a:TextWriter{TextWriter _;public a(TextWriter w){_=w;}public override void Write(char v){_.Write(v=='4'?'5':v);} public override Encoding Encoding{get{return _.Encoding;}}}").CompiledAssembly.GetTypes()[0].GetConstructor(new[] { typeof(TextWriter) }).Invoke(new object[] { Console.Out }));
    }

    Console.WriteLine("{0}+{1}={2}", 2, 2, 2 + 2);
}

 

解釋下

就是重定義了輸出到控制台的TextWrite對象 讓4變成5  也沒什么意義

總是比新聞那個版本高級一點的...呵呵~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM