Jump to content

Need help with C#

Little Bear

I came across a bit of an issue when I tried to specify an instance, and it thinks i'm trying to do an "escape sequence".

 

 

Here is the line of code:   

                                                                                                                                   The error lies here |    

                                                                                                                                                                  |

                                                                                                                                                                  \/

PerformanceCounter perftemperatureCount = new PerformanceCounter("Thermal Zone Information", "\_TZ.TZ00");

Motivation is where, and what you make of it.

 

“It is relatively unusual that a physical scientist is truly an atheist. Why is this true? Some point to the anthropic constraints, the remarkable fine tuning of the universe. For example, Freeman Dyson, a Princeton faculty member, has said, ‘Nature has been kinder to us that we had any right to expect.'”  Albert Einstein

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, Mr_KoKa said:

"\_TZ.TZ00"

If you aren't about to escape _ character then try  "\\_TZ.TZ00" \ is escape character that tries to escape following character, \\ will be just single backslash in result.

Thanks!

Motivation is where, and what you make of it.

 

“It is relatively unusual that a physical scientist is truly an atheist. Why is this true? Some point to the anthropic constraints, the remarkable fine tuning of the universe. For example, Freeman Dyson, a Princeton faculty member, has said, ‘Nature has been kinder to us that we had any right to expect.'”  Albert Einstein

Link to comment
Share on other sites

Link to post
Share on other sites

You can also use a verbatim string literal by placing the @ symbol in from of the quoted text.

@"\_etc"

This removes the need to escape characters, except the double quote character which is added like so

@"Example: ""Hi"" they said."

It also allows you to write multiline strings that keep line breaks and spacing intact.

@"Hello
    World"

With C# 6, they introduced interpolated strings which you can also combine with verbatim strings.

Stackoverflow: How do you use verbatim strings with interpolation?

Link to comment
Share on other sites

Link to post
Share on other sites

@madknight3 Explained it well.

 

An even more fun (and less known) trick using the "@" sign is that it allows you to use C# keywords as variable names. I never recommend this, but it does work. You can write things like this:

 

void Main()
{
	var @null = "HEY";
	var @if = "WHAT R U DOIN";
	var @object = "PLZ STAHP";
	
	Console.WriteLine($"{@null} {@if} {@object});
}

Once again, don't do this without a very, very good reason. It's just a neat example of how consistent the "@" signifies the idea of doing something verbatim.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×