By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,992 Members | 1,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,992 IT Pros & Developers. It's quick & easy.

Creating a static class

P: n/a
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
May 12 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Tina" <ti**********@nospammeexcite.com> wrote:
I'm using C# 1.1..............
[...]
Why? Are static classes not allowed??


Static classes are a C# 2.0 feature.

-- Barry
May 12 '06 #2

P: n/a
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.

May 13 '06 #3

P: n/a
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";
}
}

May 13 '06 #4

P: n/a
"static class" is available only form C# 2.0 onwards.

--

Cheers,
Gaurav
http://www.edujini.in
-------------------
"Tina" <ti**********@nospammeexcite.com> wrote in message
news:Oe**************@TK2MSFTNGP02.phx.gbl...
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

May 13 '06 #5

P: n/a
Chris <ch************@cns-service.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.