Wednesday, June 08, 2005 8:51 AM
After playing with C# 2.0 I noticed something funny about static classes.
According to the updated C# 2.0 specification (March 2005), static classes
are classes that are not intended to be instantiated and which contain only static
blah…blah…blah. If so then suppose
to be if I make my class as static then automatically all its methods and properties
will also be static BUT that's not the
case here. Making a class static will look something like this:
using
System;
namespace
DotNet
{
public
static class Settings
{
public
static
string GetName()
{
return
"MyName";
}
}
}
Notice that I still have to declare the GetName() method as static.
Why can’t I make it something like this?
using
System;
namespace
DotNet
{
public
static class Settings
{
public
string GetName()
{
return
"MyName";
}
}
}
Declaring a class static is already understood that it can’t be instantiated anyway.
Now assuming that we have a class with ONLY ONE method, when calling GetName() method
what is the difference between my two examples below?
Sample 1:
using
System;
namespace
DotNet
{
public
static class Settings
{
public
static
string GetName()
{
return
"MyName";
}
}
}
And
Sample 2:
using
System;
namespace
DotNet
{
public
class Settings
{
public
static
string GetName()
{
return
"MyName";
}
}
}
Calling the GetName() method in both samples would look
like this:
Sample 1 – DotNet.Settings.GetName();
Sample 2 – DotNet.Settings.GetName();
If maybe Microsoft can change it to something like this
it would be great:
using
System;
namespace
DotNet
{
public
static
class Settings
{
public
string GetName()
{
return
"MyName";
}
}
}