C# Negating a Forward Slash by a Back Slash

Forward Slashes and BackSlashes have different Interpretations in programming languages.  For Instance in C#, the Back Slash is used for commenting out Code. So, if you want to comment out code  in C#, you would do something like this-

// int test_variable=5;

That is two forward slashes at the beginning of the line. This is all and good until one works with URL’s.

Everyone knows that a complete URL would appear to be like this-

string url=”https://www.msn.com/en-us”;

A url contains the protocol followed by two forward slashes and the domain name. If you compare the code the URL with the code above, what do you notice?

You should notice that the URL contains “two forward slashes” which is actually the syntax for commenting out code in C# !! Clearly a conflict of Interest!

Which essentially means that if a compiler comes across the declaration of the string URL, it would only read until here

string url=”https:

And it will Ignore the rest of the line, since the compiler thinks that the code is commented out.

How can this be fixed? How can we tell the compiler that the forward slashes in the URL do not Intend to be a comment but it is actually a part of the String?

All we have to do is to add a Back Slash in the front of each forward slash so that the compiler knows that it is not a comment.

This is how it should be-

string url=”https:/\/\www.msn.com/en-us”;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s