0

I've been learning C# for a while. Wrote a few programs etc. I have not dealt with advanced concepts, but when studying other peoples' code I notice that there are certain lines of code that I do not understand why they are there. In the example I understand I know it is importing a DLL but am not sure what this line of code is called? Is it a statement? Where can I learn more about them?

Trying to learn about something without knowing what it is called is impossible :)

Example:

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
Sicarius
  • 73
  • 8
  • 1
    `DllImport` is for using unmanaged code in .Net framework which is a managed framework. Read more about [Using the DllImport Attribute](https://msdn.microsoft.com/en-us/library/aa984739%28v=vs.71%29.aspx) – Habib Feb 05 '15 at 15:35
  • [DllImport Attribute](https://msdn.microsoft.com/en-us/library/aa984739%28v=vs.71%29.aspx) – Izzy Feb 05 '15 at 15:35
  • Brilliant, thank you. Also, I forgot to ask what is [STAThread]? Why is it between brackets? – Sicarius Feb 05 '15 at 15:35
  • 1
    http://stackoverflow.com/questions/1361033/what-does-stathread-do – Habib Feb 05 '15 at 15:36
  • 2
    You're expected to search before asking. Had you done so, you'd have found a ton of pages on the Web with answers to your questions. – dandan78 Feb 05 '15 at 15:45
  • @dandan78 I did try to search. But I did not know what to look for, hence why I am here. – Sicarius Feb 05 '15 at 15:59
  • @dandan78 - searching is difficult for square brackets. And for DllImport and STAThread, you need to know that DllImportAttribute and STAThreadAttribute are the correct matches. – Hans Kesting Feb 05 '15 at 15:59
  • 1
    @HansKesting Not really. Searching for `DllImport` alone provides plenty of results that explain what's going on here, or at a minimum use other terms that can also be searched for. The same can be said of `STAThread`. – Servy Feb 05 '15 at 16:06
  • 2
    If you had searched "DllImport", the first item in Google's search results is the MSDN article for "Using the DllImport Attribute - MSDN - Microsoft" The first result for "STAThread" is a link to the "What does [STAThread] do?" StackOverflow article that is linked. It can be difficult to search for something that you don't know to search for, but in this case, you knew that you could look for "DllImport." For future reference, you'll do yourself a favor if you go just one step further before posting (very) basic questions such as this. – K. Alan Bates Feb 05 '15 at 16:09

2 Answers2

4

but am not sure what this line of code is called?

Anything in [ ] is an Attribute which provides the compiler or a running program that uses reflection to gain insights/hints which provide a meta view of the object which comes after its (the []) declaration.

See Attributes Tutorial for an introduction.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
1

Maybe this link will be usefull for you.

The relevant paragraphs on what the attributes are from the page are here:

Attribute Basics

Attributes are generally applied physically in front of type and type member declarations. They're declared with square brackets, "[" and "]", surrounding the attribute such as the following ObsoleteAttribute attribute:

[ObsoleteAttribute] The "Attribute" part of the attribute name is optional. So the following is equivalent to the attribute above:

[Obsolete] You'll notice that the attribute is declared with only the name of the attribute, surrounded by square brackets. Many attributes have parameter lists, that allow inclusion of additional information that customizes a program even further. Listing 16.1 shows various ways of how to use the ObsoleteAttribute attribute.

and on Microsoft's MSDN page, there is a description of the use of that particular attribute.

The DllImport attribute is very useful when reusing existing unmanaged code in a managed application. For instance, your managed application might need to make calls to the unmanaged WIN32 API.

For more in depth information, a simple search on Google or the canonical sources on MSDN which would illustrate the purpose, usage and code examples.

stephenbayer
  • 12,373
  • 15
  • 63
  • 98
mineroot
  • 1,607
  • 16
  • 23