Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the .NET runtime add underscores to my string?

Tags:

c#

.net

settings

I store my application settings the C# way (Properties.Settings.Default.Save();). The settings are then stored by the C# runtime in the folder:

C:\Users\UserName\AppData\Local\My_Company_Name

The strange thing is that I entered "My Company Name" as the Company-property in Visual Studio ([assembly: AssemblyCompany("My Company Name")]).

So, where do the underscores come from? I've seen other apps creating folders with blanks...

like image 353
Boris Avatar asked Jun 16 '11 11:06

Boris


People also ask

Why is underscore used in C#?

The underscore before a variable name _val is nothing more than a convention. In C#, it is used when defining the private member variable for a public property.

What does & _ mean in VB?

The underscore is the line continuation character. It allows the concatenation to include a different line. Like so: x = "Hello " & "World" x = "Hello " & _ "World" 'this won't compile (pre vb.net 2010, anyway) x = "Hello " & "World"


1 Answers

The problem is that you are adding a property named "My Company Name". That is not a valid member name in C# so it is automatically transformed for you into a valid name: "My_Company_Name"

Same thing happens if you create a new project named "My Project". The default namespace of your project will not be "My Project" because its not valid. The default namespace VS will create for you will be "My_Project".

like image 113
InBetween Avatar answered Oct 22 '22 03:10

InBetween