twamn
twamn

Reputation: 676

C# class definition unknown

I am looking through some C# code and I am seeing something I cannot figure out near the class definition. Here is a sample of what I am seeing.

[MethodImpl(MethodImplOptions.Synchronized)]
public void AddTag(RTag tag)
{
    this.tags.Add(tag)
}

What the heck is the first line doing or stating? I have not been able to track it down in any of my reference books.

Thanks!

Upvotes: 4

Views: 211

Answers (4)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391336

Before I point you to the documentation for the class, a tip when seeing attributes like this, and this is an attribute:

[SomeName]

or

[SomeName(...)]

then typically, the actual class name is SomeNameAttribute. When using attributes, if the class name ends with the word Attribute, you can leave that ending out.

The class in question for your example is most likely MethodImplAttribute, though I see you might have spelled it wrong, missing the ending L letter.

Upvotes: 2

dtb
dtb

Reputation: 217293

The first line is an attribute, i.e. meta data attached to the method.

The MethodImplAttribute specifies the details of how a method is implemented. In particular, MethodImplOptions.Synchronized

Specifies that the method can be executed by only one thread at a time. Static methods lock on the type, whereas instance methods lock on the instance. Only one thread can execute in any of the instance functions, and only one thread can execute in any of a class's static functions.

Upvotes: 6

James Conigliaro
James Conigliaro

Reputation: 3829

It is marking the method in such a way that it can only be called from one method at a time:

http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions(VS.71).aspx

This is equivelant to performing a lock at the start of the method and releasing the lock at the end of the method.

Upvotes: 1

vladhorby
vladhorby

Reputation: 378

[MethodImp(methodImpOptions.Synchronized)] Is an attribute applied to the method... probably it is defined in a referenced library.

Upvotes: 0

Related Questions