none
Static vs Instance to copy stream instances RRS feed

  • Question

  • This might be the newbie question here but I’m trying to copy two Streams using utility method.  In the example below there may be 1000+ instances of the Worker class. And in each worker class copies one stream into another stream using utility. The stream could be FileStream, MemoryStream or even NetwrokStream.

    What would be the best option here, should i use Static Copy method or Instance Copy Method?

    Public class Worker
    {
     Public void DoWork()
     {
    	// Assume we have instance of source & target stream here
    
            // Should i use Static 
            UtilityStatic.Copy(source,target);  	
    
            //or should i use Instance
            UtilityInstance u = new UtilityInstance();
            u.Copy(source,target); 
     }
    }
    
    
    public abstract class UtilityStatic
    {
            public static void Copy(Stream source, Stream target)
            {
                int bufferSize = 10240;
                source.CopyTo(target, bufferSize);        
            }     
    }
    
    
    public class UtilityInstance
    {
            public void Copy(Stream source, Stream target)
            {
                int bufferSize = 10240;
                source.CopyTo(target, bufferSize);        
            }     
    }



    • Edited by lax4u Tuesday, January 7, 2014 10:37 PM
    Tuesday, January 7, 2014 10:35 PM

Answers

All replies

  • There's no reason to use UtilityInstance.  It just requires an extra line of code and creates an extra object instance.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, January 7, 2014 10:39 PM
  • Thanks

    But don't you think when 1000+ instances of Worker classes try to use same static method at the same time, will cause any performance issue.

    Wednesday, January 8, 2014 4:43 PM
  • No.  There's no performance difference except that the instance method requires you to allocate a bunch of instances, and the static method does not.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Wednesday, January 8, 2014 5:02 PM