简体   繁体   中英

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")]

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> ).

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM