views:

413

answers:

6
+14  Q: 

What are [] in C#?

For example:

    [TestFixtureSetUp]
    public void Init()
    {
        GetTestRepo(false);
    }

[TestFixtureSetUp] in this example, what does it do? From my experience, [] usually refers to lists.

+2  A: 

Attributes. You can find more information here.

Yuriy Faktorovich
+2  A: 

It is an Attribute

Gregoire
+35  A: 

Attributes. They are a way to add metadata about methods/properties/interfaces/classes/namespaces for inspection at runtime.

Your example adds the TestFixtureSetUpAttribute to a method. This allows the test runner to determine which method in your class to run when setting up a text fixture.

The test runner loads your test assembly into memory at runtime. It then enumerates through the classes defined within your assembly that have been marked with a particular attribute (whatever NUnit uses to mark a test class). The runner now knows what classes to instantiate to run tests. It then looks through the methods defined in the class for a method that will be run to set up the test fixture. It searches each method for the attribute you asked about. Once it finds that method it knows to run that method before running tests/each test (whichever it means in NUnit).

Attributes are all about adding information about a method that you can search for at runtime. Its the kind of thing where if you don't need 'em you don't miss 'em, but when you DO need 'em OMFG is it great that they're available.

(In C#, you can omit the "Attribute" from the type name. The compiler knows you're talking about, for instance, "SerializableAttribute" when you say [Serializable])

Will
What's that you've done with the URL there? What does www.google.com/url do? I haven't seen it before.
P Daddy
@pdaddy Probably an artifact from my search. Fixed it.
Will
+1  A: 

It's an attribute. Sort of like MetaData for the class/function you use it on.

Mikael Svenson
A: 

They are called attributes (analogous to Java annotations) and they are compiled in as meta data.

Maxwell Troy Milton King
A: 

You appear to be recreating a basic VB/C# faq by asking one question at a time in SO. To add to that, it does appear to be a good way to get reputation.

To answer the TITLE, [] are the symbols used to

  • index into arrays
  • enclose an attribute (metadata) on a method
  • one other thing I can't remember

VB.Net uses the <> characters for the same purpose on metadata, but uses () to indicate array access.
Java uses a leading "@" for the same purpose

If you didn't have those special characters, the compiler couldn't interpret what you wrote. Not sure what else you want to know. Do you want to know the meaning of TestFixture?

Andrew Backer
Should he put many questions into one instead?
EricSchaefer
He should read a faq. Google the *title* of his questions, you'll find the answer in the first hit or two. Not to mention that the actual answer given does not match the question, and the question does not really relate to the title.
Andrew Backer
You appear to be putting comments where answers go, try reading the FAQ.
Neil N
Responding to the comment above. Did I go too meta for your taste?
Andrew Backer