what are ["foo bar"] lines in c#? [duplicate] - c#

This question already has answers here:
What are attributes in .NET?
(11 answers)
Closed 6 years ago.
So I'm coming over from Java, and I've been seeing (and using), a ton of these:
["foo bar"]
void method(param params)
{
..code things
}
Can someone explain to me what they are? I don't even know what they're called, so I can't manage to figure it out from documentation.

They are called .NET Attributes. You can read about it for example here: What are attributes in .NET?

They are called Attributes.
There are many uses, such as:
Declare tests methods
Add description on enum values
Dependency Injection with MEF
You can do things with reflection
and so on.

They are attributes. You can use them to decorate your code.
The libraries providing the attributes usually use reflection to get this decorated (meta) information from your code at runtime.
For example, the XmlSerializer uses them a lot.

Related

Is there an advantage in declaring "this" on every reference to itself in a script in C#? [duplicate]

This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
Closed 4 years ago.
I'm looking at code from another programmer and style differences aside, they do something I don't.
On every single reference to the class' own variables and methods, they precede it with this. (ie. this.Init() x Init() )
On my side, I've mostly used this to pass a reference to the instance to other classes, or, on occasion, when I needed to differentiate from base.
Is there any particular advantage in explicitly referring to this every time?
No functional difference. Perhaps the project coding style guidelines require it, in which case you should use it.

C# Reflection: Is it possible to view the code of a constructor gained using reflection? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# Can I use reflection to inspect the code in a method?
So I am messing around with reflection and something struck me. Is it possible to view the code of a constructor obtained using the GetConstructor() method without navigating to the file and viewing it in visual studio. Is it possible for example to get the ConstructorInfo object and then print out the code that is associated with that constructor to the console window? Might seem a silly question but I have never really needed to look into reflection before so I am pretty new to it and am not sure as to how much it is capable of.
Also as a side note what is it that determins which constructor will be used as the default? Is it just the one with the least parameters?
Basically, code is only available in form of MSIL instructions. See Can I use reflection to inspect the code in a method? for more info.

C# attribute usage: only allow attributes on a property with specific data type [duplicate]

This question already has answers here:
Allow a custom Attribute only on specific type
(5 answers)
Closed 9 years ago.
I've created a few attributes for properties. now I want to limit these attributes to properties with a certain data type? the idea is, a compiler error will be thrown if it is assign to a different type. is this possible?
if not, then I guess i'll have to check it on runtime.
You could write a custom FxCop/ Code Analysis rule to check for this.
FxCop is integrated in VS 2010 under the name "Code Analyis", you can change the ruleset in the project properties.
No, this is not possible.
A good workaround is to build a unit test which will traverse all classes, their properties and check for this additional applicability criteria. It's not that comfortable as a compile-time error message, but will serve the purpose. In case you have some kind of build automation infrastructure, the quality assurance level will be the same in practice.

Using private static methods Instead of private methods In C# [duplicate]

This question already has answers here:
What is better? Static methods OR Instance methods
(7 answers)
Closed 9 years ago.
When its recommended to use a private static method instead of a private [instance] method?
EDIT:
i am looking for a good (or best practice) about that .i am wondering Is this technic used by microsoft or not?does anybody know something about that ? i coudnt find any blog ,article or sample source code that explains this topic.
any help would be highly appreciated.
Static word in the beginning of the method declaration, basically is a sign of stateless, so what is happening inside is a pure action, or at least should be.
If you want to use private static: use it like API functions of your class that just make some calculations/reports... and not change the actual state of the object, which basically is done by instance methods.
This is an expected way of implementing, which doens't mean that is mandatory, but as it's expected, it will help other developers understand your code, and help understand code to you after a couple of years, when you will come back to your project and already have forgot everything.
Regards.

Attributes in .net [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
When should I use attribute in C#?
Hi, I am trying to understand how Attributes in .net works.
As we all know Attributes are of two types metadata and context attributes.
Metadata attributes: it allows some data to be attached to a class or method. This data becomes part of the metadata for the class, and can be accessed via reflection.
Firstly, why do we need custom attributes please give examples and How is that information attached with that class and how will it be interpreted.
Custom attributes: Please explain this and explain the flow how is that custom class which is derived System.Attribute is executed and how will that information be useful to the current class or method which uses that attribute. (best example is Validation Block is applied as attributes to the property or methods and it will be automatically validated. how is this possible).
I have the basic understanding of attributes and how it works and looking at the process how that works.
Thanks in advance.
I think these tutorials might help you:
http://oreilly.com/catalog/progcsharp/chapter/ch18.html
http://msdn.microsoft.com/en-us/library/aa288454(v=vs.71).aspx
http://www.dotnetjohn.com/articles.aspx?articleid=273

Categories

Resources