472,353 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Creating a static class

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
5 2984
"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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Piro | last post by:
I have a class that I want to make accessible to a web service. This class does some work in its constructor method and sets some class variables...
6
by: Davinci_Jeremie | last post by:
Hi Newbee here to C# I have a simple questions... In a Hello world example how does the class object Hello exist with out creating it? I come...
22
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg....
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a...
7
by: Brett Romero | last post by:
I need a static version of a class that can be referenced anywhere as a singleton and the same class that can be used as instances. Can this be...
1
by: Kev | last post by:
Gidday, I am stuck trying to create a COM Callable Wrapper for the class (shell only) below. As you can see I have tried to define my...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some...
2
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an...
6
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display my array of objects in a GUI. How do I get JLabel to refer to the data in my objects? I've read my textbook and some tutorials...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.