Unnecessary comma in enum declaration [duplicate] - c#

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
.NET Enumeration allows comma in the last field
public enum SubPackageBackupModes
{
Required,
NotRequired //no comma
}
public enum SubPackageBackupModes
{
Required,
NotRequired, //extra unnecessary comma
}
Since both compile, is there any differences between these declarations?

I prefer second syntax because if you will add addition member to your enum you will have only one line difference in SCM.

No, there is no difference.
This was allowed in C++ also, and that continues. I guess it is easier with the comma, since you may comment out the last enum element and it is easier for code-generation tools.

Related

What is #property_name declaration means in C#? [duplicate]

This question already has answers here:
What's the use/meaning of the # character in variable names in C#?
(9 answers)
Closed 1 year ago.
I was browsing sources for microsoft .netcore runtime and came across these lines of code
as you can see they are using # symbol infront of every error message getter like #Error_InvalidFilePath.
My question is, what is this language feature that is being used here?
And, Where can I read more about it?
Thanks
The # is a way to use reserved words as names. E.g. the variable class could be used as variable name like #class.
For non reserved names this won't add anything. But of course you don't know which names are reserved in the future. Your code example is generated code, which should preferably work for newer language versions and so the # makes sense there.
See docs

Having created an enum, how would I be able to assign them a decimal type value in a get method? [duplicate]

This question already has answers here:
Can you use Enum for Double variables?
(6 answers)
Closed 3 years ago.
I am trying to create a program in where I am trying to give/assign a created enumeration list, a monetary value to be returned. However I am unsure how I can do this.
public decimal GetAccessoriesCost()
{
?????????
}
public enum Accessories
{
None,
StereoSystem,
LeatherInterior,
StereoAndLeather,
ComputerNavigation,
StereoAndNavigation,
LeatherAndNavigation,
All
}```
I want Stereo to be 20.20
and Leather to be 10.10
The enums in C# (unlike Java) have to be cardinal (something that is basically an integer or a whole number). see this answer for more details Can you use Enum for Double variables?
The reason enums have this constraint is to be closer to c++ and c where the enums are a fancy way to group integer constants in a namespace and probably generate a sequence.

What is the use of an empty statement [duplicate]

This question already has answers here:
C# Empty Statement
(13 answers)
Closed 5 years ago.
I've come across this example of an empty statement in a C# textbook.
Code:
public void empty()
{
;
}
Some quick googling found that it's a redundant feature and I can't see the use of this as it seems pointless?
I was curious to know when this would've been useful and if it's still used to date even though it's obsolete?
In the given example it is pointless and/or cosmetic.
The empty statement is "useful" in places where a statement is required but you have nothing to do, like
while (condition_with_side_effects) ;
Because of the side effects required, this will not match with most coding guidelines or best practices.
Consider it a leftover from C.

Significance of text inside square brackets [duplicate]

This question already has answers here:
What does square bracket [] mean in the below code?
(2 answers)
Closed 8 years ago.
Hello, can you please explain me what is the significance of [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("activityid")] in the following code?
[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("activityid")]
public Microsoft.Xrm.Sdk.EntityReference ActivityId
{
get
{
return this.GetAttributeValue<Microsoft.Xrm.Sdk.EntityReference>("activityid");
}
set
{
this.OnPropertyChanging("ActivityId");
this.SetAttributeValue("activityid", value);
this.OnPropertyChanged("ActivityId");
}
}
I searched for this thing and I got many posts which gave me answer as the ones in square brackets are Attributes in C#. But, then attributes are related with methods. Over here, ActivityId doesn't seem to be a method. So, how can [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("activityid")] act as an attribute?
Is it related to C# or it has got something to do with CRM?
The confusion comes from your statement about attributes only being valid on methods. Attributes can be valid on items specified in the AttributeTargets enum:
http://msdn.microsoft.com/en-us/library/system.attributetargets.aspx
This then puts you back to the answer being "they are attributes". That attribute has simply been applied to a property.

Why string.format? [duplicate]

This question already has answers here:
String output: format or concat in C#?
(32 answers)
Why use String.Format? [duplicate]
(7 answers)
Closed 9 years ago.
Why shouldn't we simply use
string s=product.Name+" has been saved";
instead of:
string s=string.Format("{0} has been saved", product.Name);
One naive reason would be that it helps to prevent exactly the string formatting issue that you've presented in your original (unedited) question i.e.
string s=product.Name+"has been saved";
requires an extra space. The format method aids readability.
You could do that, no one say that you cannot. But mainly for readability, the second approach is prefered. It's even more obvious as soon as you concat more than 2 strings, it gets really messy, hard to read and mantain.
If you have many strings that you want to add, each + operation create new string.
For adding many strings you can use StringBuilder Class or String.Format

Categories