title: Memory allocation for regular class static class static method instanve method and data member on heap and stack c#?
body: <p>Most of the book never talk about how a program execute and how memory is allocated for them on stack and heap. when data stored on stack and when on heap.</p>
  
  <p>Suppose i have one regular class like </p>
  
  <pre><code>public class MyClass
  {
      int Age=0;
      string strName="None"
      Double Salary=0;
  
      public void Data1()
      {
          Age=30;
          strName="Robin";
          Salary=3500;
      }
  }
  </code></pre>
  
  <h2>Question</h2>
  
  <p>1) so for the above class how memory will be allocated. when program run the memory will be allocated or when we create instance then memory will be allocated. when we call <code>Data1()</code> through instance then what happen and how memory will be allocated. memory will be allocated for function call or for data member initialization? tell me how much memory will be allocated for <code>age</code>, <code>name</code> and <code>salary</code>. memory will be allocated on stack or heap.</p>
  
  <pre><code>public class MyClass
  {
      static int y=0; 
      static string strComp="None"
      int Age=0;
      string strName="None"
      Double Salary=0;
  
      public void Data1()
      {
          Age=30;
          strName="Robin";
          Salary=3500;
      }
  
      public static void Data3()
      {
           y=50;
           strComp="Hello";
      }
  }
  </code></pre>
  
  <p>2) how and when memory is allocated for static data member and function. when we call like
  <code>MyClass.Data3()</code> then memory will be allocated or when we just run the apps. memory is allocated on heap or stack?</p>
  
  <p>3) how memroy is allocated for static class. static class stored on heap or stack...if stack then why?</p>
  
  <p>here i asked asked couple of question please explain in detail. thanks.</p>