C# '@' Before a String

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 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

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 the @ symbol before a variable name mean in C#?

The @ symbol allows you to use reserved word. For example:

int @class = 15;

The above works, when the below wouldn't:

int class = 15;

What does the @ prefix do on string literals in C#

@ is not related to any method.

It means that you don't need to escape special characters in the string following to the symbol:

@"c:\temp"

is equal to

"c:\\temp"

Such string is called 'verbatim' or @-quoted. See MSDN.

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.

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

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)

Using the literal '@' with a string variable

I don't think you have to worry about it if you already have the value. The @ operator is for when you're specifying the string (like in your first code snippet).

What are you attempting to do with the path string that isn't working?



Related Topics



Leave a reply



Submit