c# basics

T

Tem

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
 
J

Jon Skeet [C# MVP]

Tem said:
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
...
...
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?

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.
 
J

Jeroen

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
 
J

Jon Skeet [C# MVP]

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
 
T

Tem

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?
 
J

Jon Skeet [C# MVP]

Tem said:
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.
 
J

Jon Skeet [C# MVP]

Tem said:
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()
 
A

Andrew

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
 
P

Peter Duniho

static Cat c = new Cat();

This took me off guard. Can this be done?

Sure. In the correct context, it can.
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]

Right. C# doesn't have static local variables.

You could have:

class StaticTest
{
static Object o = new Object();

public static void Main() { }
}

Pete
 
K

KWienhold

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
 
J

Jon Skeet [C# MVP]

Andrew said:
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();
}
}

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

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?

Declaring static variables has always been in C#.
 
A

Andrew

Yap.. thanks Jon, Peter.

I misread the field to local variables.... absolutelty...

--
-- Andrew



Jon Skeet said:
Andrew said:
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();
}
}

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

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?

Declaring static variables has always been in C#.
 

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