This question already has answers here:
Use the long reserved word as a variable name in C#
(5 answers)
Closed 6 years ago.
So I have this simple problem that I'm struggling with. Consider this code:
namespace Foo
{
public class Bar
{
public void Test(string object)
{
}
}
}
This function throws a syntax error because object is a keyword in C#. Is there a way to solve that? In my real code I have a framework that uses function's signature to create an API and I should really use object name as parameter.
Use # before parameter name #object to use keyword as identifier
public void Test(string #object)
From C# Language Specification 2.4.2 Identifiers:
The rules for identifiers given in this section correspond exactly to those recommended by the Unicode Standard Annex
31, except that underscore is allowed as an initial character (as is
traditional in the C programming language), Unicode escape sequences
are permitted in identifiers, and the “#” character is allowed as a
prefix to enable keywords to be used as identifiers.
The # symbol allows you to use reserved word. For example:
int #class = 15;
The above works, when the below wouldn't:
int class = 15;
Related
This question already has answers here:
What's the # in front of a string in C#?
(9 answers)
Closed 5 years ago.
Sometimes I find that many people use #string for locate a directory than use string even I though that they are same.
for example :
I have variable that called direct
string direct = "C:\\Users";
And then,I type :
System.IO.Directory.CreateDirectory(#direct);
I think it's same with
System.IO.Directory.CreateDirectory(direct);
But,what's difference between #direct and direct?
The # prefix allows you to use a reserved keyword as a variable name.
The following is an error:
string string = "C:\\Junk";
but the following is allowed (although a very bad idea):
string #string = "C:\\Junk";
The # prefix can also be used as a verbatim string literal:
string thefolderpath = #"C:\Junk";
If I had to guess, I would say the original programmer decided to use #string, #int etc. for temporary variables, as a style choice. A style choice, it must be said, akin to wearing socks with sandals.
This question already has answers here:
What does the # symbol before a variable name mean in C#? [duplicate]
(4 answers)
Closed 8 years ago.
I'm seeing this very often recently - what does it mean?
public void IPersistenceRepository<TEntity> : IDisposable {
|
v
public IEnumerable<TEntity> Where(Expression<Func<TEntity, bool>> #predicate);
}
or
public IDomainEventHandler<TEvent> : IEventHandler {
|
v
public void Handle(TEvent #event);
}
From the C# Language Specification, § 2.4.2 Identifiers (C#)
:
The prefix "#" enables the use of
keywords as identifiers, which is
useful when interfacing with other
programming languages. The character #
is not actually part of the
identifier, so the identifier might be
seen in other languages as a normal
identifier, without the prefix. An
identifier with an # prefix is called
a verbatim identifier.
Its a method of escaping identifiers which are also keywords.
It's just part of the variable name, but allows you to use reserved key words of the C# language, as names and identifiers.
This has to do with that that the .Net framework designed not for specific language. What would happen if code that was written in other .Net language would use C#'s reserved key word as an identifier name? How can use access that identifier from C#? the answer is that you would add # before the parameter name.
You can use this feature regardless of using code from other languages (as in your code, you define a variable called event although event is reserved key word), and even regardless of using key words (predicate is not a key word, yet, you can still prefix it with #).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between String and string
When I run:
char c1 = 'a';
Console.WriteLine(c1);
and when I run:
Char c2 = 'a';
Console.WriteLine(c2);
I get exactly the same result, a.
I wanted to know what is the difference between the two forms, and why are there two forms?
The result is exactly the same. Both represent the same type, so the resulting executables are completely identical.
The char keyword is an alias in the C# language for the type System.Char in the framework.
You can always use the char keyword. To use Char you need a using System; at the top of the file to include the System namespace (or use System.Char to specify the namespace).
In most situations you can use either a keyword or the framework type, but not everywhere. For example as backing type in an enum, you can only use the keyword:
enum Test : int { } // works
enum Test : Int32 {} // doesn't work
(I use int in the example, as You can't use a char as backing type for an enum.)
Related: Difference between byte vs Byte data types in C#
As far as I know, C# char type keyword is simply an alias for System.Char, so they refer to the same type.
The keyword char is an alias of the System.Char type in C#.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
In C# what is the difference between String and string
String vs string in C#
Hi,
Can anyone tell me the difference between the two lines of code below:
public const String sample = "Sample";
public const string sample2 = "Sample2";
Both "String" and "string" are of System.String.
Thanks in advance.
These data types are exactly the same, as string is just an alias for the class String. If you have a look, there are similar capitalized and non capitalized versions of int, float and similar classes.
Have a look here for a more detailed answer.
There are the same
string is an alias in the C# for .Net System.String, like int is for Int32, long is for int64 and etc. C# string replaced by System.String during compilation
No difference what so ever - in fact you can write the following code:
String sample = "Sample";
string sample2 = sample;
Both maps to the same IL string type
String vs string in C#
String stands for System.String and it is a .NET Framework type. string is an alias in the C# language for System.String. Both of them are compiled to System.String in IL (Intermediate Language), so there is no difference. Choose what you like and use that. If you code in C#, I'd prefer string as it's a C# type alias and well-know by C# programmers.
String is CTS type but string is c# string object.
You can use String to any of dot net language.
both are the same
string -> c# type which gets converted to
String -> .net type
String is the .NET class for the CLR built-in string type. string is the C# language identifier that maps to the CLR String type. They are the same thing.
"string" is actually an alias for System.String. They're the same.
Try:
typeof(string) == typeof(String) == typeof(System.String)
Nothing really, in C# the type keywords actually are synonyms for the types. So int = System.Int32 short = System.Int16 and string = System.String.
They have the keywords because they are easier to remember and programmers coming from other languages like c/c++ would also be familiar with these types.
Anyway, look at the C# keyword reference and you can find these things out. This was taken from the string keyword reference.
The string type represents a string of Unicode characters. string is an alias for String in the .NET Framework. Strings are immutable--the contents of a string object cannot be changed after the object is created.
I have some client data that I am reading in, and I've defined an Enum for one of the values, so I can use Enum.Parse(type, somestring).
The problem is they just added a new value: "public". Is it possible to define an enum value that is also a reserved word?
I.E.:
public enum MyEnum {
SomeVal,
SomeOtherVal,
public,
YouGetTheIdea
}
If not I guess I'll be writing a parse method instead.
You can prepend a # to the variable name. This allows you to use keywords as variable names - so #public.
See here.
From the C# spec:
The prefix "#" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character # is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an # prefix is called a verbatim identifier. Use of the # prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.
yes, prefix the name with an #. i.e. #public
If you capitalize public to Public it won't be recognized as a keyword. Keywords are case sensitive.
As a general practice, however, it's a bad idea to use names that are keywords (even when they differ by case) as it can cause confusions, or even subtle defects if the keyword is accidentally used in place of the identifier.
It's also possible to use the # in certain contexts (like variable or member declarations) to use reserved words as non-keywords. However, it's not a common practice and should only be a means of last resort, when you can't use a different name.
So in your case you could also use #public to use the reserved word as an enum identifier.
If you chose to use #, be aware that the symbol is only used in your source code to differentiate the identifier from the reserved word. To the outside world (and in methods like Enum.Parse()), the name of the enum value is simply public.
It's not really a great idea to do this though. Instead, add a bit more info to the enum:
PublicAccess etc
In VB.Net use square braces [...] to delineate a keyword as an identifier.
Example:
Public Sub Test(ByVal [public] As String)
MessageBox.Show("Test string: " & [public])
End Sub
For VB.NET do the following:
Public Enum MyEnum As Integer
Disabled = 0
[New] = 1
[Public] = 2
Super = 4
[Error] = 5
End Enum