Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# square brackets and greater/less than

Tags:

c#

.net

f#

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")]
like image 211
Jwosty Avatar asked Dec 04 '22 15:12

Jwosty


2 Answers

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

like image 151
Reed Copsey Avatar answered Dec 07 '22 23:12

Reed Copsey


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

[<assembly: AssemblyTitle("MyProject")>]
do ()
like image 23
Daniel Avatar answered Dec 08 '22 01:12

Daniel