class declarations - basic question

  • Thread starter Thread starter Jason Shohet
  • Start date Start date
J

Jason Shohet

I noticed that there are two ways I declare classes:

1. public class xxxx { }
2. Somewierdclass myClass;

The 2nd example exists provided I have a 'using' statement at the top that
includes that class from some place. I am wondering, why does the 2nd way
not require a scope (ie public) ? And why doesn't it require the keyword
'class'. Is it because 'class' denotes an empty.NET class, whereas
Somewierdclass is something that already exists.

I am guessing thats also why the 1st example has brackets & the 2nd doesn't.
Because the first example is an empty .NET class and the members of the
class are defined in the { } 's. But the 2nd example is a ready-built class
from somewhere else, and so we can't add on members to a pre-existing class
coming from somewhere else, hence no brackets?

Thanks for any correction / confirmation of the above
Jason Shohet
 
1. public class xxxx { }
2. Somewierdclass myClass;

The 2nd example exists provided I have a 'using' statement at the top
that includes that class from some place.

You could also place:

SomeNamespace.Somewierdclass myClass;

instead of

using SomeNamespace;

Somewierdclass myClass;



I am wondering, why does the 2nd way
not require a scope (ie public) ?

Each variable where you don't declare a scope is automaticly set to private.

Somewierdclass myClass == private Somewierdclass myClass


And why doesn't it require the keyword
'class'. Is it because 'class' denotes an empty.NET class, whereas
Somewierdclass is something that already exists.

You use 'class' only when you are declaring a class... Not when defining

A variable of that type.

You would use

Public class myClass

{

....

}

And then somewhere else:

myClass myVar = new myClass

Or something similar.

Hope this helps,

Saso
 
The first example is how you DEFINE a class. Think of a class as a blueprint
to a house. A blueprint is not the same thing as an ACTUAL house. This class
definition is the blueprint.

The second example is how you define a datatype, that will hold a class. In
actuality, you would need to do this:

Someweirdclass myClass = new Someweirdclass();

This says that the myClass variable will be of type "Someweirdclass" and it
also "instantiates" an "instance" of that class.

A class definition is like the blueprints to a house - while an instance is
much like an actual house. You could say that a house is an "instance" of
what the blueprint described.

hth
 
Thank you, great explanation.
Actually Saso I think you meant, you use 'class' only when you are defining
a class... Not when declaring a class (that already has been defined
somewhere else) ?
 
Back
Top