Jwosty
Jwosty

Reputation: 3634

C# square brackets and greater/less than

In C#, you might see things such as:

[<DllImport("myUnmanagedDLL.dll")>]

or a similar line (but without the greater/less than symbols):

[assembly: AssemblyTitle("MyProject")]

I know that the first is called an attribute (it has gt and lt signs) and can be used to add a sort of metadata to methods, types, etc, but what does the syntax of the second mean? I'm trying to translate something with this syntax to F# -- namely, this line:

[MonoMac.Foundation.Register("AppDelegate")]

Upvotes: 4

Views: 1195

Answers (3)

Daniel
Daniel

Reputation: 47904

In case it's helpful—in F#, assembly level attributes are typically applied to an empty do block:

[<assembly: AssemblyTitle("MyProject")>]
do ()

Upvotes: 6

Jason
Jason

Reputation: 3960

I think you're confusing C# with VB.NET syntax

In VB.NET it's <DllImport("myUnmanagedDLL.dll")> while in C# it's [DllImport("myUnmanagedDLL.dll")] without the greater than or less than signs.

The second is an assembly attribute, it's used to apply an attribute to the entire assembly, instead of just a particular class, method or property

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564373

but what does the syntax of the second mean?

This means that the attribute is being applied to the assembly, not to a type (class or struct) or member.

In F#, the line you're trying to translate should be:

[<MonoMac.Foundation.Register("AppDelegate")>]

Without seeing more, it's impossible to tell where this should be applied, however (a type, a method, etc). I suspect this would go on your type definition in F#, though, given that this is typically used on a C# class.

On a side note, [<DllImport("myUnmanagedDLL.dll")>] is not valid C# - that's F# syntax. C# uses [Attribute] for attributes (and VB.Net uses <Attribute>).

Upvotes: 7

Related Questions