Im learning c#. want to make sure my understanding of certain concepts is
correct
- is struct basically a sealed static class?
- what exactly does the keyword virtual do?
- what is the best practice for assigning accessors(public/private)? limit
scope as much as possible?
- if you add the keyword static to a class does that make every methods in
that class static? or is this not allowed?
I've seen code where it like
public class Cat
...
...
just like a normal class
when it instantiates the class, it adds the static keyword
static Cat c = new Cat();
What does this mean? how is this different from a static class?
Thanks for clearing up some confusions
Tem
> Im learning c#. want to make sure my understanding of certain concepts is
> correct
>
> - is struct basically a sealed static class?
No. It's a value type.
See http://pobox.com/~skeet/csharp/references.html
> - what exactly does the keyword virtual do?
It allows a member to be overridden. Look up inheritance and
polymorphism for more details.
> - what is the best practice for assigning accessors(public/private)? limit
> scope as much as possible?
Yes, generally.
> - if you add the keyword static to a class does that make every methods in
> that class static? or is this not allowed?
Making a class declaration static *forces* all members to be declared
(explicitly) as static.
> I've seen code where it like
> public class Cat
[quoted text clipped - 5 lines]
> static Cat c = new Cat();
> What does this mean? how is this different from a static class?
That's declaring the variable "c" to be static - the class is just a
normal class.
Unfortunately, concepts like "what static means" aren't best described
in newsgroup posts - you really need a book or a tutorial for that sort
of level of detail.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Andrew - 06 Mar 2008 04:24 GMT
Jon,
static Cat c = new Cat();
This took me off guard. Can this be done? I tried
using System;
class StaticTest {
public static void Main() {
static Object o = new Object();
}
}
but
static Object o = new Object();
failed. [error CS0106: The modifier 'static' is not valid for this item]
Is this not for .NET 2.0 and does it work in 3.x?
--Andrew
>> Im learning c#. want to make sure my understanding of certain concepts is
>> correct
[quoted text clipped - 38 lines]
> in newsgroup posts - you really need a book or a tutorial for that sort
> of level of detail.
Peter Duniho - 06 Mar 2008 07:18 GMT
> static Cat c = new Cat();
>
> This took me off guard. Can this be done?
Sure. In the correct context, it can.
> I tried
>
[quoted text clipped - 11 lines]
>
> failed. [error CS0106: The modifier 'static' is not valid for this item]
Right. C# doesn't have static local variables.
You could have:
class StaticTest
{
static Object o = new Object();
public static void Main() { }
}
Pete
Jon Skeet [C# MVP] - 06 Mar 2008 07:41 GMT
> static Cat c = new Cat();
>
[quoted text clipped - 7 lines]
> }
> }
That's trying to declare a *local* variable as static. Try this
instead:
using System;
class StaticTest
{
static object o = new object();
static void Main()
{
}
}
> but
>
[quoted text clipped - 3 lines]
>
> Is this not for .NET 2.0 and does it work in 3.x?
Declaring static variables has always been in C#.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Andrew - 06 Mar 2008 07:57 GMT
Yap.. thanks Jon, Peter.
I misread the field to local variables.... absolutelty...

Signature
-- Andrew
>> static Cat c = new Cat();
>>
[quoted text clipped - 31 lines]
>
> Declaring static variables has always been in C#.
> is struct basically a sealed static class?
A struct and a class are very different. Use google or msdn to read
some introductions on both, and you'll see how.
> what exactly does the keyword virtual do?
It allows you to override the functionality of a method in a derived
class, thus allowing a derived class to do something different in the
method body than is done in the parent class.
> what is the best practice for assigning accessors(public/private)?
> limit scope as much as possible?
Usually yes, there are a few C# coding guidelines out there that will
help you with that. Google is your friend (or live search ;))
> if you add the keyword static to a class does that make every
> methods in
C# doesn't have static classes, as far as I know. You can make members
of a class static. Personally, I regard a class with only static
members as more or less a static class.
Regards,
Jeroen
Jon Skeet [C# MVP] - 05 Mar 2008 13:25 GMT
<snip>
> > if you add the keyword static to a class does that make every
> > methods in
> C# doesn't have static classes, as far as I know. You can make members
> of a class static. Personally, I regard a class with only static
> members as more or less a static class.
Static classes were introduced in C# 2. They have the following
features:
1) All members must be static
2) No default constructor is produced
3) The generated class is abstract and sealed
4) The compiler prevents you from using the static class as a "normal"
class in various situations, e.g.:
o No variables of that type
o No arrays of that type
o No parameters of that type
o No return values of that type

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Tem - 05 Mar 2008 13:48 GMT
>> what exactly does the keyword virtual do?
> It allows you to override the functionality of a method in a derived
class, thus allowing a derived class to do something different in the
method body than is done in the parent class.
I thought a derived class can override any method of the base class. what is
an abstract class then? just a signature?
> <snip>
>
[quoted text clipped - 17 lines]
> o No parameters of that type
> o No return values of that type
Jon Skeet [C# MVP] - 05 Mar 2008 14:07 GMT
> >> what exactly does the keyword virtual do?
>
[quoted text clipped - 3 lines]
>
> I thought a derived class can override any method of the base class.
No - only virtual members.
> what is an abstract class then? just a signature?
An abstract class is allowed to (but doesn't have to) contain abstract
members. An abstract member is declared just as a signature - the
implementation is provided in a direct class.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Tem - 05 Mar 2008 14:30 GMT
> >> what exactly does the keyword virtual do?
>
[quoted text clipped - 3 lines]
>
> I thought a derived class can override any method of the base class.
>No - only virtual members.
How does extension methods in c#3 fit into all this?
>> >> what exactly does the keyword virtual do?
>>
[quoted text clipped - 11 lines]
> members. An abstract member is declared just as a signature - the
> implementation is provided in a direct class.
Jon Skeet [C# MVP] - 05 Mar 2008 14:35 GMT
> >No - only virtual members.
>
> How does extension methods in c#3 fit into all this?
They're just syntactic sugar around static method calls. Whenever an
instance method is accessible and applicable, that will be used. If no
instance methods can be used, the compiler checks for accessible and
applicable extension methods.
They are "sort of" overridden by target type, in that an extension on
DerivedType will be used before an extension on BaseType, assuming both
are available and you're trying to call
myDerivedInstance.SomeMethodWhichIsntReallyAnInstanceMethod()

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Tem - 06 Mar 2008 00:08 GMT
one more question
when do you use the keyword 'new' to override a method?
>> >> what exactly does the keyword virtual do?
>>
[quoted text clipped - 11 lines]
> members. An abstract member is declared just as a signature - the
> implementation is provided in a direct class.
Peter Duniho - 06 Mar 2008 00:25 GMT
> one more question
> when do you use the keyword 'new' to override a method?
You don't. That hides a method, rather than overrides it.
Pete
KWienhold - 06 Mar 2008 07:36 GMT
> An abstract class is allowed to (but doesn't have to) contain abstract
> members. An abstract member is declared just as a signature - the
> implementation is provided in a direct class.
In addition, you can't create an instance of an abstract class, it can
only be used to derive other classes from it.
Kevin Wienhold