473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3056
"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 in its various methods. The problem I am having...
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 from object pascal where everything object is...
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. #include <iostream.h> class test { public: void...
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 situation where I need to set up my program to access one...
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 done without basically creating the same class twice...
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 interface and it would be all good if I wasn't passing 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 additional functionality. The reason I need this is...
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 ANT script that gives the following output: ...
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 online I just cannot get this. Plus all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.