Do class members occupy memory?

A class usually consists of member variables and methods. When we instantiate the class, memory is allocated for member variables of the class. Do member methods also use memory? Where are these methods stored?

+5
source share
2 answers

Say we have the following class:

public class Person
{
   public string Name { get; set; }

   public Person(string name)
   {
        Name = name;
   }

   public string SayName()
   {
      string hello = "Hello! My name is ";
      return hello + name;
   }
}

Person p = new Person("John");
string yourName = p.SayName();

The function SayName()is located on Call Stack, and the object Person pand its properties ( Name) will remain in memory until Garbage Collectionit enters and clears it.

() , , , Reader Connection. Reader Connection, using.

-:

using(DatabaseConnection dbConn = new DatabaseConnection()
{
    // Process your calls and data
}
// The object is Disposable and it resources are cleared 
+3

s , - , . / , . , .

0

All Articles