4

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!

3
  • This is written with a capital letter? That sound fishy. Commented Nov 13, 2009 at 18:59
  • @Pierre, he probably wrote this outside of Visual Studio, since public was also capitalized. Commented Nov 13, 2009 at 19:02
  • Yeah, you are probably right. Commented Nov 13, 2009 at 19:03

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.