1

I have a calculation class file where I am doing something. I made an object x of the calculation class in .cs file of aspx and called it in aspx using server tag. Now I am getting hits for that page. Is that page is going to use the a single object for that class or for every hit will it create a new object? Is this a good practice of doing coding?

calculation.cs

using system;

public calculation()
{
 //some decelerations
}

public string getProduct()
{
return (A*B*C).toString();
}

page.aspx.cs

public calculation cal = new calculation();

page.aspx

<%# cal.getProduct()%>  
vishu9219
  • 761
  • 6
  • 14

1 Answers1

0

It is completely dependent upon how you have instantiated the object.

If it's a singleton with a static variable scope then it will be created once for each app domain. If the app recycles then it will be created again. ASP .NET Singleton

If it's a singleton that's stored in the HttpContext, then it will be created once per request. See example here of how to do this: http://dotnetslackers.com/community/blogs/simoneb/archive/2006/08/21/The-ASP.NET-Singleton_2D00_per_2D00_Request-pattern.aspx

If it's a regular variable, then it will be created once every time that page code is called, even it is on the same request.

Community
  • 1
  • 1
NotMe
  • 87,343
  • 27
  • 171
  • 245