Interesting way to bugger up your code
-
Try the following code if you want to confuse other people (and yourself!):
using System;
class Test
{
const string A = "Hello!";/// <summary> /// I need a :beer:! /// </summary> static void Main() { string B = A; string C = "Hello!"; buggerUpMyCode(B); Console.WriteLine("This is bad enough..."); Console.WriteLine("A={0}", A); Console.WriteLine("B={0}", B); Console.WriteLine("A==B: {0}", A==B); Console.WriteLine("C={0}", C); Console.WriteLine("A==C: {0}", A==C); Console.WriteLine("\\"Hello!\\" = {0}", "Hello!"); Console.WriteLine("A==\\"Hello!\\": {0}", A=="Hello!"); Console.WriteLine("A==\\"Yellow\\": {0}", A=="Yellow"); // // :wtf: // buggerUpMyCodeSomeMore("True", "Faux"); buggerUpMyCodeSomeMore("False", "Vrai "); Console.WriteLine("\\n...but what about this!"); Console.WriteLine("A==B: {0}", A==B); Console.WriteLine("A==\\"Test\\": {0}", A=="Test"); // // :omg: // } /// <summary> /// D'Oh! /// </summary> unsafe static void buggerUpMyCode(string A) { fixed(char\* pA = A) { pA\[0\] = 'Y'; pA\[5\] = 'w'; } } /// <summary> /// Hate world. Revenge soon. Take out on everybody. /// </summary> unsafe static void buggerUpMyCodeSomeMore(string A, string B) { fixed(char\* pA = A) { for (int i=0; i < A.Length && i < B.Length; i++) pA\[i\] = B\[i\]; } }
}
Just imagine the hours of "fun" you could cause for other developers by misusing this in a component! * Maybe it's time to review those code access security permissions. Or maybe it's just time I got a life! ;P * Note: In accordance with Microsoft specifications, "fun" means "mind-numbing, folicle-wrenching agony".
-
Try the following code if you want to confuse other people (and yourself!):
using System;
class Test
{
const string A = "Hello!";/// <summary> /// I need a :beer:! /// </summary> static void Main() { string B = A; string C = "Hello!"; buggerUpMyCode(B); Console.WriteLine("This is bad enough..."); Console.WriteLine("A={0}", A); Console.WriteLine("B={0}", B); Console.WriteLine("A==B: {0}", A==B); Console.WriteLine("C={0}", C); Console.WriteLine("A==C: {0}", A==C); Console.WriteLine("\\"Hello!\\" = {0}", "Hello!"); Console.WriteLine("A==\\"Hello!\\": {0}", A=="Hello!"); Console.WriteLine("A==\\"Yellow\\": {0}", A=="Yellow"); // // :wtf: // buggerUpMyCodeSomeMore("True", "Faux"); buggerUpMyCodeSomeMore("False", "Vrai "); Console.WriteLine("\\n...but what about this!"); Console.WriteLine("A==B: {0}", A==B); Console.WriteLine("A==\\"Test\\": {0}", A=="Test"); // // :omg: // } /// <summary> /// D'Oh! /// </summary> unsafe static void buggerUpMyCode(string A) { fixed(char\* pA = A) { pA\[0\] = 'Y'; pA\[5\] = 'w'; } } /// <summary> /// Hate world. Revenge soon. Take out on everybody. /// </summary> unsafe static void buggerUpMyCodeSomeMore(string A, string B) { fixed(char\* pA = A) { for (int i=0; i < A.Length && i < B.Length; i++) pA\[i\] = B\[i\]; } }
}
Just imagine the hours of "fun" you could cause for other developers by misusing this in a component! * Maybe it's time to review those code access security permissions. Or maybe it's just time I got a life! ;P * Note: In accordance with Microsoft specifications, "fun" means "mind-numbing, folicle-wrenching agony".
Weird:confused: Care to explain why? Output is:
This is bad enough...
A=Yellow
B=Yellow
A==B: True
C=Yellow
A==C: True
"Hello!" = Yellow
A=="Hello!": True
A=="Yellow": False...but what about this!
A==B: Faux
A=="Test": Vrai
Press any key to continueOh well, just another reason to stick with C++.
CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!
-
Weird:confused: Care to explain why? Output is:
This is bad enough...
A=Yellow
B=Yellow
A==B: True
C=Yellow
A==C: True
"Hello!" = Yellow
A=="Hello!": True
A=="Yellow": False...but what about this!
A==B: Faux
A=="Test": Vrai
Press any key to continueOh well, just another reason to stick with C++.
CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!
Welcome to string interning :) What happens is the C# compiler automatically adds any string literals to a table of strings. Each value in this table is a unique string and all hardcoded uses of that string point back to that particular string in the table. When he uses unsafe to code to modify the actual values of the string he is making modifications to the string table. The only way to accomplish this is to use unsafe code because part of the basic contract of a string object is that its contents do not change.
James Sig code stolen from David Wulff