简体   繁体   中英

class Monitor as normal class

I am reading this link: http://eycenlearnstation.blogspot.nl/ .

They are using on the blog visual studio 2005

and somewhere in the blog they describe this line:

 Monitor myMonitor = new Monitor(ipAddr);

But Monitor is a static class. So my question is:

Was in Visual studio 2005 the class Monitor not a static class?

Or is it just a mistake in the blog?

Thank you

I don't know which Monitor class is talking about the whole article, but it's not the Monitor threading approach. System.Threading.Monitor has always had no public constructor.

In .NET 1.x wasn't a static class because there were no static classes at all (they were introduced in .NET 2.0).

You can see that on this old MSDN document (What's New in the C# 2.0 Language and Compiler) :

Static Classes Static classes are a safe and convenient way of declaring a class containing static methods that cannot be instantiated. In C# version 1.2 you would have defined the class constructor as private to prevent the class being instantiated.

And, @Jcl has added in some comment:

In fact, .NET 1.1's System.Threading.Monitor did have a private constructor (from the doc: An instance of the Monitor class cannot be created. )

The System.Threading.Monitor class is and has always been static on the .NET Framework ( documentation for .NET 2.0 ), however, in the linked tutorial, it clearly shows a different class named Monitor is being created.

This is a quote from the page:

What's the deal with the Monitor object? What namespace is that? Well that's the class we're going to create next.

And there's an implementation below that text:

class Monitor
{
  // Will store the IP address passed to it
  IPAddress ipAddress;

  // The constructor sets the IP address to the one retrieved by the instantiating object
  public Monitor(IPAddress address)
  {
      ipAddress = address;
  }
  /* ... etc. ... */

Update : as per other answer, it seems it was not static in .NET 1.1 (because .NET 1.1 didn't have static classes, however all of its methods were also static) ... then again, the rest of the answer is right, it's just not refering to the .NET Framework class

I'm seeing this for the class definition on the page; it isn't static, so it can be instantiated with the new keyword:

class Monitor
{
    // Will store the IP address passed to it
    IPAddress ipAddress;

    // The constructor sets the IP address to the one retrieved by the instantiating object
    public Monitor(IPAddress address)
    {
        ipAddress = address;
    }
...

The System.Threading.Monitor class was not static in .NET 1.0 as there was not static classes at that version, it was marked as static in .NET 2.0 when static classes get introduced:

Source MSDN

This is to answer you question. As the Monitor class used in the topic, it is a user defined class named Monitor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM