This question already has answers here:
How to define constants in Visual C# like #define in C?
(7 answers)
Closed 7 years ago.
Is there an equivalent to this in C#?
#define TypeConstant int
int main()
{
TypeConstant x = 5;
}
Thank you very much!
Edit: I am not sure how this is related to defining a regular constant, I have explicitly written type constant, not a constant value! Read before you vote guys!
It's not exactly the same, but types can be aliased by using the using Directive:
using TypeConstant = System.Int32;
As Kyle points out in the comments, you need to use the full type name here (e.g. System.Int32) instead of the C# aliases (e.g. int).
Related
This question already has answers here:
How can I use a reserved keyword as an identifier in my JSON model class?
(3 answers)
How do I use a C# keyword as a property name?
(1 answer)
Closed 3 years ago.
I am currently working on an c# application that uses the spotify api. For the parsing of the Newtonsoft stuff I am using Newtonsoft.Json. But when receiving a track, the json includes a key explicit, and explicit is a keyword. So my question is, if there is a way to give the track class a member called explicit
Use the # prefix to escape reserved keywords.
var #explicit = ...
This question already has answers here:
Difference between Convert.ToString() and .ToString()
(19 answers)
variable.ToString() vs. Convert.ToString(variable)
(2 answers)
Closed 7 years ago.
Can anyone please explain me what is the difference between these two string Type Casting method.
Type 1
int i = 90;
string b = i.ToString();
Type 2
int i = 90;
string b = Convert.ToString(i);
I want to know the major difference of using the two different approach. The 1st one is to handle the null value,we can use 'Convert.ToString(); '. And moreover I get to know that '.ToString()' can be override.
Can anyone can explain how. And which one is good to use.
Thanks in Advance.
This question already has answers here:
String to enum conversion in C#
(8 answers)
C# using numbers in an enum
(8 answers)
Closed 9 years ago.
HI I have the following enum
public enum Priority : byte
{
A=1,
B+ = 2,
B=4,
C=8,
D=16,
E=32
}
I want to add B+ in the enum but it is giving me error
You can add user friendly description for enum like below :
enum MyEnum
{
[Description("This is black")]
Black,
[Description("This is white")]
White
}
Ref. Link : How to have userfriendly names for enumerations?
How about using a valid identifier like B_Plus?
Yes. It's giving you an error because your code is wrong. You can't make "B+" an enum value because there's a plus sign. Same reason you can't declare int B+. Use a different name.
You won't be able to use + as a name identifier because it's a math operator or string concatenator... it can't be used with enums. Use an alternative syntax, or use an alternative approach. You could consider a state design pattern:
http://www.dofactory.com/patterns/PatternState.aspx#_self2
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C# what is the difference between String and string
I couldn't find the info anywhere, but I'm sure it's just a simple answer. Are they interchangeable??
Yes, they are identical. string is just the C# name for a System.String, which you can also use. See MSDN:
The string type represents a string of Unicode characters. string is an alias for System.String in the .NET Framework.
It's personal preference, but I always use string over String. I guess I like the blue color over the, uh, turquoise color offered by the default syntax highlighting.
You can see the other aliases under the C# value types on MSDN (e.g. int is really System.Int32 while long is really System.Int64).
You get this question a lot because in java there is a difference between for example Integer and int (pointer allocation). In C# there is no difference between String and string, nor there is a difference between Int32 and int etc..
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
a curious c# syntax
So I've seen some code around and a few of them use a ? after the type, like this:
private Point? loc = null;
So I'm wondering if Point? is different than Point (can't put a question mark at the end of my sentence or I'll confuse you guys ... :] ). The language I'm using is C# by the way.
T? is a shorthand (in C#) for Nullable<T> - so Point? is another way of writing Nullable<Point> or example.
See sections 1.3 and 4.1 of the C# 3 language spec - and various other places, to be honest - for more details. See the docs for System.Nullable<T> for more information from the framework side of things. Or read chapter 4 of C# in Depth :) (Unfortunately it's not one of the free chapters.)
(This question is bound to be a duplicate, but I don't have the energy to find it right now.)
Point? is the same as Nullable<Point>. It allows you to assign null to value types, such as structs.
It means the type can accept its' value and null.