3 advantages of @ symbol in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 6142 red flag
Rating: 4.33 out of 5  
 3 vote(s)

Three important use of @ symbol in C# code

3 advantages of @ symbol in C#

Here we will see three different advantages of @ symbol in C# application.

In front of C# keyword

We can use @ symbol in front of any keyword in C# and we can suppress general meaning of that keyword. For example, in below program we have used @ symbol in front of if and while, very known keyword of C# but using @ symbol we can use them as normal variable. To control if and while loop we have used if and while keyword simultaneously.

using System;
using System.Collections;
using System.Globalization;
 
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            int @if; // use if as an identifier and suppress it's general meaning
            int @While = 0;
 
            for (@if = 0; @if < 10; @if++)
Console.WriteLine("Value is " + @if);
 
            while (@While < 10)
            {
Console.WriteLine("Value is " + @While);
@While++;
            }
            Console.ReadLine();
        }
    }
}
 


To preserve white space

We can preserve white space using @ symbol before long string. In below example we have declared a long string with lot of white space and newline character. As we have used @ symbol the string preserve its state as it is.

using System;
using System.Collections;
using System.Globalization;
 
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            String Name = @"Thsi                             is
                            my
                            name
                            ";
            Console.WriteLine(Name);
            Console.ReadLine();
        }
    }
}
 


To insert special character within string

If we want to insert any special character within string then we have to use @ symbol in front of string. In below example we have declare a string which contents few special character like ‘and \ symbol. This is very useful to prevent SQL injection attack in application.

using System;
using System.Collections;
using System.Globalization;
 
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            String str = @"Thsi ' is \n complex string";
            Console.WriteLine(str);
            Console.ReadLine();
        }
    }
}


 Conclusion:-

 Those are the three advantages of @ symbol in c# application

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 8/9/2013 | Points: 25
Very good information, even I was not aware about first one.

Keep it up!

Login to post response

Comment using Facebook(Author doesn't get notification)