Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Non static class with only static methods

Non static class with only static methods

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpcomquestion
13 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DaveyM69
    wrote on last edited by
    #1

    I have noticed looking at MS source that they regularly do this for a class with only static methods instead of creating a static class:

    public class ClassName
    {
    private ClassName()
    { }

    public static void Method()
    {
        // ...
    }
    

    }

    Is there any benifit to this that I'm missing over a static class?

    Dave
    Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

    M P B G K 5 Replies Last reply
    0
    • D DaveyM69

      I have noticed looking at MS source that they regularly do this for a class with only static methods instead of creating a static class:

      public class ClassName
      {
      private ClassName()
      { }

      public static void Method()
      {
          // ...
      }
      

      }

      Is there any benifit to this that I'm missing over a static class?

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      Perhaps so the class can be inherited? just a guess

      Don't vote my posts down just because you don't understand them - if you lack the superior intelligence that I possess then simply walk away

      N 1 Reply Last reply
      0
      • D DaveyM69

        I have noticed looking at MS source that they regularly do this for a class with only static methods instead of creating a static class:

        public class ClassName
        {
        private ClassName()
        { }

        public static void Method()
        {
            // ...
        }
        

        }

        Is there any benifit to this that I'm missing over a static class?

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        P Online
        P Online
        PIEBALDconsult
        wrote on last edited by
        #3

        Pre version 2? Backward compatibility?

        G 1 Reply Last reply
        0
        • D DaveyM69

          I have noticed looking at MS source that they regularly do this for a class with only static methods instead of creating a static class:

          public class ClassName
          {
          private ClassName()
          { }

          public static void Method()
          {
              // ...
          }
          

          }

          Is there any benifit to this that I'm missing over a static class?

          Dave
          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          COM Interop? COM requires a public constructor without parameters, and there it is. And isn't it possible to access class methods through an instance with COM? When I remember those times when I was VB programmer correctly, that was possible, but I am not sure.

          1 Reply Last reply
          0
          • D DaveyM69

            I have noticed looking at MS source that they regularly do this for a class with only static methods instead of creating a static class:

            public class ClassName
            {
            private ClassName()
            { }

            public static void Method()
            {
                // ...
            }
            

            }

            Is there any benifit to this that I'm missing over a static class?

            Dave
            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            G Offline
            G Offline
            Gregory Gadow
            wrote on last edited by
            #5

            I think this is laziness on the part of MS. If (when?) the class definition changes in the Framework, the documentarians won't have to root around in the code to make sure that every method is still static.

            K 1 Reply Last reply
            0
            • P PIEBALDconsult

              Pre version 2? Backward compatibility?

              G Offline
              G Offline
              Gregory Gadow
              wrote on last edited by
              #6

              Which sounds plausable, but why allow the class to be instanced (by making it non-static) when it can never be instantiated (because the constructor is private)? Backwards compatibility is going to break anyway, just in a much more awkward spot.

              P 1 Reply Last reply
              0
              • D DaveyM69

                I have noticed looking at MS source that they regularly do this for a class with only static methods instead of creating a static class:

                public class ClassName
                {
                private ClassName()
                { }

                public static void Method()
                {
                    // ...
                }
                

                }

                Is there any benifit to this that I'm missing over a static class?

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                K Offline
                K Offline
                Karthik A
                wrote on last edited by
                #7

                You could use static methods if the functionality of the method is not dependent on the instance properties/methods. Consider the following class as an example. There is a static "Parse" method that takes in a string and return a new instance of "Data". This static function "does not" depend on any of the instance properties/methods of the Data class. But it just creates an instance of the Data class for you to use. So, any other class that wishes to create an instance of Data (which has a ',' seperated int's) can do so by calling Data.Parse(_stringValue).

                public class Data
                {
                private List a;

                public Data()
                {
                  a = new List();
                }
                
                public Data(int\[\] nums)
                {
                   a = new List();
                   // add to a
                }
                
                public static Data Parse(string nums)
                {
                   // say nums = "1,2,3
                   string\[\] \_nums = nums.Split(",".ToCharArray());
                   List iList = new List();
                   foreach(string s in nums)
                      iList.Add(int32.Parse(s));
                   return new Data(iList.ToArray());
                }
                

                }

                PS - This is a just a trivial example of the usage of static methods in a non-static class. Edit - If there are only static methods in a non-static, it might be useless, but it could also be for futuristic reasons. They could add to this class more instance properties / methods in the future that would use these static methods.

                Cheers, Karthik

                1 Reply Last reply
                0
                • G Gregory Gadow

                  I think this is laziness on the part of MS. If (when?) the class definition changes in the Framework, the documentarians won't have to root around in the code to make sure that every method is still static.

                  K Offline
                  K Offline
                  Karthik A
                  wrote on last edited by
                  #8

                  I don't think your argument is valid. Static methods in non-static classes have their own uses. I have given one such (trivial) example.

                  Cheers, Karthik

                  G 1 Reply Last reply
                  0
                  • K Karthik A

                    I don't think your argument is valid. Static methods in non-static classes have their own uses. I have given one such (trivial) example.

                    Cheers, Karthik

                    G Offline
                    G Offline
                    Gregory Gadow
                    wrote on last edited by
                    #9

                    I agree: I use static methods in non-static classes all the time. But the question was about non-static classes with nothing BUT static methods.

                    K 1 Reply Last reply
                    0
                    • G Gregory Gadow

                      I agree: I use static methods in non-static classes all the time. But the question was about non-static classes with nothing BUT static methods.

                      K Offline
                      K Offline
                      Karthik A
                      wrote on last edited by
                      #10

                      Yes, I noticed that later. So I also updated my answer to take that in to account (see EDIT in my answer)...

                      Cheers, Karthik

                      1 Reply Last reply
                      0
                      • G Gregory Gadow

                        Which sounds plausable, but why allow the class to be instanced (by making it non-static) when it can never be instantiated (because the constructor is private)? Backwards compatibility is going to break anyway, just in a much more awkward spot.

                        P Online
                        P Online
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        Private, schmivate; I can call a private constructor or anything else -- which is why they introduced static classes in .net 2.

                        1 Reply Last reply
                        0
                        • M musefan

                          Perhaps so the class can be inherited? just a guess

                          Don't vote my posts down just because you don't understand them - if you lack the superior intelligence that I possess then simply walk away

                          N Offline
                          N Offline
                          Nitin Singh India
                          wrote on last edited by
                          #12

                          A class with a single private default constructor cannot be inherited. It will always need some way to instantiate the parent...

                          M 1 Reply Last reply
                          0
                          • N Nitin Singh India

                            A class with a single private default constructor cannot be inherited. It will always need some way to instantiate the parent...

                            M Offline
                            M Offline
                            musefan
                            wrote on last edited by
                            #13

                            Yes well spotted. Thou I am not assuming the op sample code is 100% correct still

                            I may or may not be responsible for my own actions

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups