Newbie

C

Chak

I have 4 newbie questions regarding C# . Hope they are not too raw :-

1) As a migrant from COBOL to Java, i was amazed to see a main() method of a
class creating an object of its own class ! I used to ask myself how a
class can instantiate itself from within ? Is this also possible / the
done thing in C# ?

2) C++ had a concept of copy constructors to handle pass by values (though i
don't know the exact details of that construct). How does C# manage to
overcome that problem without the need for copy constructors ?

3) If the 'object' type is the parent of all types, is a 'class' type also
inherited from an 'object' type ?

4) I came across something called 'marker ' interfaces whose methods need
not be implemented by the implementing class. These are just a formal
implementation without the need to define the interface methods.Are there
such interface types and if so , isn't this a contradiction ? I thought ALL
interfaces are supposed to be implemented ? Please correct me if i am wrong
on this .

Regards,

Chak.
 
B

Bruce Wood

1) Static methods, of which Main is one, exist outside of any class
instance. A static method can be called simply by mentioning the name
of the class, without having to instantiate anything. So, a static
method can create an instance of any class it likes, including an
instance of the class that declares it. (In fact, that's how the
Singleton pattern is implemented... "instantiating itself" from Main is
just one example of this behaviour.)

2) In C# you have to make an explicit call to a class's Clone method
(assuming that it implements ICloneable). There is no behind-the-scenes
magic as in C++. However, C# does add "struct"s (different from
classes), which have value semantics.

3) Yes and no. There is a class called Type, which contains, at runtime
all of the information about a class or struct. It is a subclass of
object. "class" itself, though, is a keyword in the language, not a
type. (Types that you declare as "class"es, of course, are
automatically subclasses of "object".)

4) All interfaces must have all of their properties, events, and
methods implemented in the class that implements the interface. That
said, if a particular interface has no properties, no events, and no
methods, then there is nothing to implement.
 
P

PiotrKolodziej

3) Only for clear. Object represents memory block.
Class represents eventuall set of those memory blocks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top