Significance of text inside square brackets [duplicate] - c#

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.

Related

Set the Enum display value C# [duplicate]

This question already has answers here:
WPF Binding a ListBox to an enum, displaying the Description Attribute
(6 answers)
Closed 5 years ago.
I want to get the Enum values into a DropDown list (WPF Binding), but the values should include characters like Spaces and Forward-Slashes which is not allowed in Enum Values, I guess there is a way to get a modified value binding or set an Attribute to each value,
This is my Enum:
public enum CurrencyPair
{
JPY_EUR = 1,
USD_NZD = 2,
EUR_AUD = 3,
}
It's definitely different than the duplicate-marked question, because yes I found the answer there, but my question was in a position that I don't know how to search for it - since I didn't know what Attribute is needed for this purpose...
You'll want to use the Description attribute from System.ComponentModel.DataAnnotations
The answers to this question have the code you need:
Enum ToString with user friendly strings

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.

Difference between ContentPropertyAttribute and ContentProperty [duplicate]

This question already has answers here:
What's the difference between [Something] and [SomethingAttribute] [duplicate]
(3 answers)
Closed 7 years ago.
I hope this wasn't asked already. But i found nothing. If something exists, thanks for the note.
The title says it all i think.
I've seen these two variants. But in my opinion it does the same. And why can i use both. Thanks for education.
// variant 1
[ContentProperty("Text")]
// variant 2
[ContentPropertyAttribute("Text")]
You can omit the word "Attribute" when writing attributes over something. The actual class is called ContentPropertyAttribute. Both of your lines do exactly the same and use the exact same attribute class.

What does the [] represent in C# [duplicate]

This question already has answers here:
What are attributes in .NET?
(11 answers)
Closed 9 years ago.
i've been developing in c# for 4 month and I still dont know what does the [] means in entity framework.
Here an example
[Column("mycolumn")]
public int Column {get;set;}
What is it functionality?
Its there another situation that i have to use it or just with entity framework?
Square brackets [ & ] mean a few different things in C#, but in this case they are saying that "Column" is an Attribute. An attribute is basically design time information that you add to classes or properties for various reasons. You can also make your own ;)

Unnecessary comma in enum declaration [duplicate]

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.

Categories