Creating a static class

T

Tina

I'm using C# 1.1..............

I add new Class item to my project. It's created as class Math

I change it to public static class Math

I get an error saying that "The modifier 'static' is not valid for this
item.

Why? Are static classes not allowed??

Thanks,
T
 
B

Bruce Wood

What do you mean by "static class"?

Do you mean a class that has only static methods and static fields, and
no public constructor? In C# 1.1 you have to make a class with only
static methods and (optionally) static fields, and give it a private
no-argument constructor.

You probably also want to declare it as "sealed" meaning that nothing
can inherit from it.
 
C

Chris

In C# 2.0, the "static" keyword can be applied to a class. Doing so
already makes it "sealed". The compiler complains if you add both.
Although everthing in the class is static, you still have to apply the
"static" keyword to all fields, properties, methods, etc.

In all versions of C# (and .NET), you can also declare a type
initializer. While a private constructor is one way of implementing
the a singleton class, it can often be achieved using the type
initializer. The type initializer can also be used to perform type
initialization logic, which could include initializing fields, etc.

By default, if a no constructor is provided a parameterless constructor
is provided for you. If a type initializer is specified, there is no
need to declare a private constructor because the "new" keyword will be
disallowed. If you want both an instance and "static" constructor, you
must explicitly declare them both.

Here's a simple example:

public class Foo
{
private static readonly _firstName;
private static readonly _lastName;
private static _fullName;

public static string FirstName
{
get
{
return _firstName;
}
}

public static string LastName
{
get
{
return _lastName;
}
}

public static string FullName
{
get
{
if ( _fullName == null )
{
if ( _firstName == null && _lastName == null )
_fullName = string.Empty;
else if ( string.IsNullOrEmpty( _firstName ) )
_fullName = _lastName;
else if ( string.IsNullOrEmpty( _lastName == null ) )
_fullName = _firstName;
else
_fullName = string.Format( "{0} {1}", _firstName,
_lastName );
}
return _fullName;
}
}

// notice that scope cannot be applied to a type initializer, nor
can it have any parameters
static Foo()
{
_firstName = "Real";
_lastName = "Foo";
}
}
 
J

Jon Skeet [C# MVP]

Chris said:
In C# 2.0, the "static" keyword can be applied to a class. Doing so
already makes it "sealed". The compiler complains if you add both.
Although everthing in the class is static, you still have to apply the
"static" keyword to all fields, properties, methods, etc.

In all versions of C# (and .NET), you can also declare a type
initializer. While a private constructor is one way of implementing
the a singleton class, it can often be achieved using the type
initializer.

They're both part of the normal singleton pattern. The point of
providing a private constructor is to prevent instantiation from other
classes.
By default, if a no constructor is provided a parameterless constructor
is provided for you. If a type initializer is specified, there is no
need to declare a private constructor because the "new" keyword will be
disallowed.

That's not true. Here's a short but complete program demonstrating
this:

using System;

class SupposedSingleton
{
static SupposedSingleton()
{
Console.WriteLine ("Static constructor");
}
}

class Test
{
static void Main()
{
// For a real singleton, this shouldn't be allowed
SupposedSingleton s = new SupposedSingleton();
}
}
If you want both an instance and "static" constructor, you
must explicitly declare them both.

Nope - the compiler takes no notice of static constructors when
considering whether or not to provide a default constructor.

From the C# 1.1 spec:

<quote>
If a class contains no instance constructor declarations, a default
instance constructor is automatically provided.
</quote>

Note the "instance" part.
 

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

Similar Threads


Top