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. C#
  4. Memory Usage

Memory Usage

Scheduled Pinned Locked Moved C#
performancehelpquestion
6 Posts 5 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.
  • S Offline
    S Offline
    student_rhr
    wrote on last edited by
    #1

    Ok so I am confused. What happends memory wise when I pass an instance of a class as a method parameter, for e.g.:

    public class ClassA
    {
    // other junk
    }
    public class ClassB
    {
      // other junk
    
      public string GetClassName(ClassA classA)
      {
        classA.OtherMethodClass();
        return classA.ToString();
      }
    }
    public class ClassC
    {
    // other junk
      ClassA a = new ClassA();
      // set a's properties
      ClassB b = new ClassB();
      string x = b.GetClassName(a);
      Console.WriteLine(x);
    }
    

    Now does a new object get created in memory when I pass the class instance? Or would any changes made to the method parameter would also effect the actual object? Just need some clarification. Thanks for your help.

    G U J 3 Replies Last reply
    0
    • S student_rhr

      Ok so I am confused. What happends memory wise when I pass an instance of a class as a method parameter, for e.g.:

      public class ClassA
      {
      // other junk
      }
      public class ClassB
      {
        // other junk
      
        public string GetClassName(ClassA classA)
        {
          classA.OtherMethodClass();
          return classA.ToString();
        }
      }
      public class ClassC
      {
      // other junk
        ClassA a = new ClassA();
        // set a's properties
        ClassB b = new ClassB();
        string x = b.GetClassName(a);
        Console.WriteLine(x);
      }
      

      Now does a new object get created in memory when I pass the class instance? Or would any changes made to the method parameter would also effect the actual object? Just need some clarification. Thanks for your help.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You only pass a reference to the object. Objects are never copied in .NET unless you specifically copy them.

      --- single minded; short sighted; long gone;

      S 1 Reply Last reply
      0
      • S student_rhr

        Ok so I am confused. What happends memory wise when I pass an instance of a class as a method parameter, for e.g.:

        public class ClassA
        {
        // other junk
        }
        public class ClassB
        {
          // other junk
        
          public string GetClassName(ClassA classA)
          {
            classA.OtherMethodClass();
            return classA.ToString();
          }
        }
        public class ClassC
        {
        // other junk
          ClassA a = new ClassA();
          // set a's properties
          ClassB b = new ClassB();
          string x = b.GetClassName(a);
          Console.WriteLine(x);
        }
        

        Now does a new object get created in memory when I pass the class instance? Or would any changes made to the method parameter would also effect the actual object? Just need some clarification. Thanks for your help.

        U Offline
        U Offline
        Urs Enzler
        wrote on last edited by
        #3

        And if you pass a ValueType like int, DateTime, ... then a copy is passed to the method, as long as you do not mark them with ref or out. The copy is then pushed to the Stack where the method gets it from.

        -^-^-^-^-^- no risk no funk ................... please vote ------>

        1 Reply Last reply
        0
        • G Guffa

          You only pass a reference to the object. Objects are never copied in .NET unless you specifically copy them.

          --- single minded; short sighted; long gone;

          S Offline
          S Offline
          student_rhr
          wrote on last edited by
          #4

          So if I make any changes to the object passed through the method parameter, those changes will also effect the actual object?

          	public class ClassA
          	{
          		public ClassA()
          		{}
          		public string Color = "orange";
          		// other junk
          	}
          
          	public class ClassB
          	{
          		public ClassB(){}
          		// other junk  
          		public string GetClassName (ClassA classA)  
          		{   classA.Color = "red";
          			classA.OtherMethodClass();
          			return classA.ToString();
          		}
          	}
          	public class ClassC
          	{
          		public ClassC()
          		{
          			// other junk  
          			ClassA a = new ClassA();  
          			
          			// set a's properties 
          			Console.WriteLine(a.Color.ToString()); // Will write orange
          
          			ClassB b = new ClassB();  
          			string x = b.GetClassName(a);  
          			
          			Console.WriteLine(a.Color.ToString()); // Will this write Red?
          			
          			a.Color = "Green";
          		}	   
          	}
          
          L 1 Reply Last reply
          0
          • S student_rhr

            So if I make any changes to the object passed through the method parameter, those changes will also effect the actual object?

            	public class ClassA
            	{
            		public ClassA()
            		{}
            		public string Color = "orange";
            		// other junk
            	}
            
            	public class ClassB
            	{
            		public ClassB(){}
            		// other junk  
            		public string GetClassName (ClassA classA)  
            		{   classA.Color = "red";
            			classA.OtherMethodClass();
            			return classA.ToString();
            		}
            	}
            	public class ClassC
            	{
            		public ClassC()
            		{
            			// other junk  
            			ClassA a = new ClassA();  
            			
            			// set a's properties 
            			Console.WriteLine(a.Color.ToString()); // Will write orange
            
            			ClassB b = new ClassB();  
            			string x = b.GetClassName(a);  
            			
            			Console.WriteLine(a.Color.ToString()); // Will this write Red?
            			
            			a.Color = "Green";
            		}	   
            	}
            
            L Offline
            L Offline
            Lutoslaw
            wrote on last edited by
            #5

            Yes it will write "red".

            Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

            1 Reply Last reply
            0
            • S student_rhr

              Ok so I am confused. What happends memory wise when I pass an instance of a class as a method parameter, for e.g.:

              public class ClassA
              {
              // other junk
              }
              public class ClassB
              {
                // other junk
              
                public string GetClassName(ClassA classA)
                {
                  classA.OtherMethodClass();
                  return classA.ToString();
                }
              }
              public class ClassC
              {
              // other junk
                ClassA a = new ClassA();
                // set a's properties
                ClassB b = new ClassB();
                string x = b.GetClassName(a);
                Console.WriteLine(x);
              }
              

              Now does a new object get created in memory when I pass the class instance? Or would any changes made to the method parameter would also effect the actual object? Just need some clarification. Thanks for your help.

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #6

              Guffa answered your question correctly for class instances: passing a class instance to a method will *NOT* copy it; you're passing a pointer to that instance. As Urs mentioned, if you pass a struct instance (say, a System.Int32, a DateTime, or your own custom struct), it *WILL* pass a copy of the struct instance to the method. In summary: class instances are passed by reference (no copying going on), and struct instances are passed by value (copying). As a side note, if you do want to pass values by reference, you can do so using the ref keyword.

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Sound The Great Shofar! The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              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