Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Please see the following code snippet....

private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

My doubt is what is mean by

My doubt is not simply on the word DllImport :-) What is the meaning of [] in c# ?
Updated 18-Oct-11 1:13am

It means that you are declaring an attribute to the method declaration (hence the '[' and ']' parts) which is saying that the method is not located in a .NET assembly: it is in a standard DLL file, in this case called user32.dll
Share this answer
Silju MC 18-Oct-11 7:16am    
OK :-) what is that marker [] mean ? And also what are the usages of [] ?
OriginalGriff 18-Oct-11 7:30am    
It means just what I said: '[' and ']' enclose an attribute declaration being associated with the next item to declare. You can use them on any declaration, not just methods, to control what happens - some attributes control visibility in the VS designer, some control serialization, and so forth.
Silju MC 18-Oct-11 7:44am    
Thank you:-) accepting your answer...
Silju MC 18-Oct-11 7:18am    
My 5+
It is the native location of the function that you are importing. User32.dll is one of the core windows dlls and ShowWindow is implemented in it.
Share this answer
Silju MC 18-Oct-11 7:15am    
OK :-) what is that marker [] mean ?
Silju MC 18-Oct-11 7:19am    
My 5+
Nicholas Butler 18-Oct-11 7:21am    
DllImport is an Attribute and this is how you apply one to a method.
That is an attribute – see this article[^] for a good introduction to them.

E: Attributes allow you to add declarative metadata to elements, in addition to the normal type system metadata included in the declaration itself. Attributes don't generally do anything, they are just static information within the assembly, but various parts of the framework (including the compiler and the OS) look at some of them (e.g. [Obsolete], [Serializable], [AssemblyVersion]) and alter their behaviour accordingly.

You can create new attributes by inheriting from Attribute, but they don't do anything – you then need to write code to inspect elements (through reflection generally) for attributes and change your logic accordingly. This can result in very elegant 'end user' code, marking fields as e.g. columns in an output data table, or to be packaged up in a TCP message, etc, but at the cost of a bit of performance and also some rather messy code in the part of the application that manages the logic based on attributes.

[DllImport] is used by the framework to cue it on what to try to load when it resolves the method (at runtime, I think, not sure about that though).
Share this answer
Silju MC 19-Oct-11 0:08am    
This is the thing i want. Thanks a lot. my 5+ and I'm accepting your answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900