Basic questions

  • Thread starter Thread starter p988
  • Start date Start date
P

p988

Someone said that: [STAThread]/[MTAThread] in Main() only effect when
dealing with COM interop in console type application." Is this correct?

What if calling a regular DLL coded in C, instead of a COM object, is this
attribute still necessary?

Under what conditions should "[MTAThread]" be used instead of "[STAThread]"?
When to use "[STAThread]" then?



In a C#'s .cs file, a class is derived as ((1), (2) and (3) are added for
explanation):

public (1) class BaseForm : (2) Form
{
(3) string myString = " Hello, world!";

public BaseForm(){
...
}
...
}

What's the default modifier above before "Form" at place (2)and (3)?
Why "public" is used at (1)? If missing what's the default modifier at (1)?


Thanks in advance!
 
p988 said:
Someone said that: [STAThread]/[MTAThread] in Main() only effect when
dealing with COM interop in console type application." Is this correct?

Yes. Further, it's only "locked" after the first call to unmanaged code. You
can set the ApartmentState property of the current thread to change it.
What if calling a regular DLL coded in C, instead of a COM object, is this
attribute still necessary?
No.

Under what conditions should "[MTAThread]" be used instead of "[STAThread]"?
When to use "[STAThread]" then?

Depends on what type of COM server you're talking to, or writing. In general
MTA should be used, but there's no "hard" rule.
public (1) class BaseForm : (2) Form
{
(3) string myString = " Hello, world!";

public BaseForm(){
...
}
...
}

What's the default modifier above before "Form" at place (2)and (3)?
Why "public" is used at (1)? If missing what's the default modifier at
(1)?

Inheritance is always public in C# (unlike C++ where it can be private), so
(2) implies public. (3) is private by default. (1) is internal (to the
containing assembly) by default, IIRC.
 
Back
Top