Code Injection works in C# too!

Given the following C# program outputting False, inject a 'malicious' line of code such that the program outputs True.

class Program
{
static void Main()
{
System.Console.Write("False");
;
}
}


Your answer should consist of a string that replaces the second semicolon, and causes the program to output True, the whole True and nothing but True (not even a newline). It must do this if stdout is printed to the console, and if stdout is redirected to a file.

• What's your objective winning criterion? – J B Jan 3 '14 at 14:14
• @JB Do puzzles need a winning criterion? Will edit. – Kendall Frey Jan 3 '14 at 14:15
• I'm mostly reacting to the code-golf remark, but the point stands: which will be the accepted answer? – J B Jan 3 '14 at 14:16
• @JB Shortest answer is best answer. – Kendall Frey Jan 3 '14 at 14:16
• Well it is code-golf, then, isn't it? – J B Jan 3 '14 at 14:18

52 characters

}static Program(){System.Console.Write(0<1);for(;;);


so the whole thing becomes:

class Program
{
static void Main()
{
System.Console.Write( "False" );
}
static Program()
{
System.Console.Write( 0 < 1 );
for ( ; ; ) ;
}
}

• Sorry for editing the question in the middle of your answer. Newlines are no longer allowed, you'll have to use Write. – Kendall Frey Jan 3 '14 at 15:16
• Ok, I've changed WriteLine to Write – Danko Durbić Jan 3 '14 at 15:18
• You could probably save some bytes by replacing your exit with for(;;);, nothing I've seen requires that the program terminates...? – Joachim Isaksson Jan 3 '14 at 16:19
• @JoachimIsaksson Yes, that meets the requirements as specified. – Kendall Frey Jan 3 '14 at 16:28
• }static Program(){System.Console.Write(0<1);for(;;); could always become }static Program(){for(System.Console.Write(0<1);;); – NPSF3000 Feb 18 '14 at 5:33

C#, 51 characters

Console.Write("\b\b\b\b\b");Console.Write("True ");


Will only work on standard output

• First thing I tried, unfortunately it won't work because of the initial WriteLine. – Vereos Jan 3 '14 at 15:09
• Now it is Write :/ I don't get how we would manage to solve this if it keeps changing... – Vereos Jan 3 '14 at 15:12
• @Vereos I wanted to remove the ambiguity of 'newline or no newline'. – Kendall Frey Jan 3 '14 at 15:13
• You don't need 2 instances of Console.Write, and you could use \r instead of 5 \bs. Console.Write("\rTrue "); – Brian S Jan 3 '14 at 21:10
• I don't think it meets the criteria. It actually outputs False first, which is then overwtitten by True later. – microbian Feb 4 '14 at 7:44

You can generalize RamonBoza's answer to still work if stdout is redirected to a file.

Assuming the program runs under Mono on Linux, with the assembly Mono.Posix.dll loaded:

if(Mono.Unix.Native.Syscall.isatty(1))
{
Console.Write("\rTrue ");
}
else
{
// Truncate the output file first
Console.OpenStandardOutput().SetLength(0);
Console.Write("True");
}


I doubt this works on Windows, but there's probably an equivalent to isatty().

83... WIP

I was hoping this'd work, but apparently "False" isn't being interned as I'd hoped in my compiler

//}unsafe static Program(){fixed(char*f="False"){*(long*)f=0x65007500720054;*(f+4)=' ';}
}unsafe static Program(){fixed(char*f="False")for(int i=5;i-->0;)*(f+i)="True "[i];


ungolfed

class Program
{
static void Main(string[] args)
{
Console.Write("False");
}

unsafe static Program()
{
fixed (char* f = "False") for (int i = 5; i-- > 0; ) *(f + i) = "True "[i];
}
}

class Program
{
static void Main()
{
System.Console.Write("False");

System.Console.Clear(); System.Console.Write("True");

}
}

• This won't work if stdout is redirected. – Kendall Frey Feb 19 '14 at 13:03

Not sure if this is accepted.
The question doesn't say anything about compiler options so I assume I can change the entry point of the app.
55 characters ,3 more than the accepted answer

}class P{static void Main(){System.Console.Write(1>0);}


Full

class Program
{
static void Main()
{
System.Console.Write("False");
}
class P
{
static void Main() { System.Console.Write(1 > 0); }
}
}

• Welcome to the community! – Erik the Outgolfer Jul 22 '16 at 9:48
• @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Thanks. This is a fantastic site!!! – George Vovos Jul 22 '16 at 9:49