What's the @ in Front of a String in C#

What's the @ in front of a string in C#?

It marks the string as a verbatim string literal - anything in the string that would normally be interpreted as an escape sequence is ignored.

So "C:\\Users\\Rich" is the same as @"C:\Users\Rich"

There is one exception: an escape sequence is needed for the double quote. To escape a double quote, you need to put two double quotes in a row. For instance, @"""" evaluates to ".

What does $ mean before a string?

$ is short-hand for String.Format and is used with string interpolations, which is a new feature of C# 6. As used in your case, it does nothing, just as string.Format() would do nothing.

It is comes into its own when used to build strings with reference to other values. What previously had to be written as:

var anInt = 1;
var aBool = true;
var aString = "3";
var formated = string.Format("{0},{1},{2}", anInt, aBool, aString);

Now becomes:

var anInt = 1;
var aBool = true;
var aString = "3";
var formated = $"{anInt},{aBool},{aString}";

There's also an alternative - less well known - form of string interpolation using $@ (the order of the two symbols is important). It allows the features of a @"" string to be mixed with $"" to support string interpolations without the need for \\ throughout your string. So the following two lines:

var someDir = "a";
Console.WriteLine($@"c:\{someDir}\b\c");

will output:

c:\a\b\c

C# '@' before a String

It means to interpret the string literally (that is, you cannot escape any characters within the string if you use the @ prefix). It enhances readability in cases where it can be used.

For example, if you were working with a UNC path, this:

@"\\servername\share\folder"

is nicer than this:

"\\\\servername\\share\\folder"

What is @ in front of a variable / identifier in C#?

In this case it's completely unnecessary, but it allows you to use any keyword as an identifier in C#. It doesn't change the meaning of the identifier at all, or how it's used - it only tells the compiler that you don't want the following characters to be recognized as a keyword.

For example:

string @int = "hello";
var @void = @int;

Using it for an identifier of claimsList suggests that whoever wrote it doesn't understand it. The fact that the identifier is for a string variable is entirely irrelevant here.

Personally I've pretty much only ever used the feature for extension methods, where I have been known to call the first parameter @this:

public static void Foo(this Bar @this)
{
return @this.Baz() * 2;
}

What does placing a @ in front of a C# variable name do?

It's just a way to allow declaring reserved keywords as vars.

void Foo(int @string)

What does `@` mean at the start of a string in C#?

It denotes a literal string, in which the '\' character does not indicate an escape sequence.

What does @ mean before a string?

It means it's a literal string, so won't treat \ as an escape character, for example. This page should help you understand it better.

in C#, What does the @ symbol do?

A string literal such as @"c:\Foo" is called a verbatim string literal. It basically means, "don't apply any interpretations to characters until the next quote character is reached". So, a verbatim string literal can contain backslashes (without them being doubled-up) and even line separators. To get a double-quote (") within a verbatim literal, you need to just double it, e.g. @"My name is ""Jon""" represents the string My name is "Jon". Verbatim string literals which contain line separators will also contain the white-space at the start of the line, so I tend not to use them in cases where the white-space matters. They're very handy for including XML or SQL in your source code though, and another typical use (which doesn't need line separators) is for specifying a file system path.

Taken from

Adding an @ symbol before a string in C#

If you are passing agrument at runtime use quotes to "close" its value within:

C:\your_console_application "arg_1_value" "arg 2 value"

this will be exctracted inside appliaction as:

args[0] -> arg_1_value

args[1] -> arg 2 value



Related Topics



Leave a reply



Submit