Jason Smith Jason Smith - 4 years ago 114
C# Question

Variables valid or not

My Question for everyone is, are these valid variables or not valid because i don't want any more errors going forward. I've tried to google and I've looked over stackoverflow for answers so now I ask the community for help with understanding what I'm doing wrong.

string First Name;
char MiddleInitial;
string #Name;
int Rank_Num;
string Street/Address;
string StreetAddress2;
string StreetAddress;
double Dim _Cost;
bool Error;
String K;

Answer Source

Identifiers in C# are subject to the following constraints:

A name must begin with a letter that could be followed by a sequence of letters, digits (0 - 9) or underscore. The first character in an identifier cannot be a digit.

It must not contain any embedded space or symbol such as? - + ! @ # % ^ & * ( ) [ ] { } . ; : " ' / and . However, an underscore ( _ ) can be used.

It should not be a C# keyword.

Source.

Therefore,

  • First Name is not valid, because it contains a space.
  • #Name is not valid, because it contains an invalid symbol.
  • Street/Address is not valid, because it contains an invalid symbol.
  • Dim _Cost is not valid, because it contains a space.

The rest of the identifiers are valid. Note that string should always be lowercase in C#.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download