Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4040542
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:45:57+00:00 2026-05-20T12:45:57+00:00

I have a hand full of methods that interact with an instance of a

  • 0

I have a hand full of methods that interact with an instance of a class. Right now I have it initialized on the page load (its cast from a session). The code goes similar to this (simplified for the sake of an example):

protected void Page_Load(object sender, EventArgs e)
{ 
    if(!isPostBack)
    {
        MyClass myClass = (MyClass)Session["myClass"];
        myMethod1(myClass);
        myMethod1(myClass);
    }
}

private void myMethod1(MyClass class)
{
}

private void myMethod2(MyClass class)
{
}

Is this the correct way to do it, or would it be better to declare it “globaly” in the page behinds class rather than pass it from method to method.

Edit

I guess my real question is: Is there a performance drawback to passing it to methods (because I assume it is being reinitialized each time) vs declaring it as a public class inside the code behind’s page class. If its declared public in the page’s code behind then I assume it’s initialized once when the page is requested.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:45:58+00:00Added an answer on May 20, 2026 at 12:45 pm

    This is a question about the scope and lifetime of the variable. A variable’s “scope” is the program text in which the variable can be referenced without any qualification, and the “lifetime” is the duration for which the variable is valid.

    In general, it is best to limit a variable’s scope as much as possible. This makes it much easier to reason about the code.

    In the example code given, there are three variables, and each of them are scoped to the individual method in which they appear. This is a good thing. Assuming the names of the methods are accurate, you can see at a glance everything that is happening with that value.

    By contrast, if you increased the scope of the variable by making it a field on the Page, then all of the methods on that page would have access to the variable. It could be difficult to reason about where it is generated, how it is mutated, and what it is used for. This makes future maintenence of the application more difficult.

    Therefore, my recommendation is to continue doing exactly as you have done here, whenever it is possible to do so.

    EDIT

    In response to the clarification of the question:

    Performance is irrelivant. You are talking about nanoseconds on a web server. Web applications are constrained by traffic over the network or Internet. There will be no noticable difference in performance by any of these options about where the variable goes.

    In general, performance is a question of algorithms and data structures over large collections of objects. In situations like these where there are no large data structures, then a human is not going see any difference.

    Code should be designed first to be correct, understandable, and maintainable. Once that is done, then performance is only a problem if it turns out to be a problem.

    Another edit

    If MyClass is a class rather than a struct then the three variables in the code which are of that type are only the size of a single reference (32- or 64-bits, depending on the platform). The object to which they all refer is only created once in the code example given.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my main Actionscript file i have a instance of a body class that
In my aspx page I have drop down list that initialy in page load
Problem : I have a hand held device that scans those graphic color barcodes
I have a problem in my android application, I have a hand page, I
An idle question on language design, see Does C# have a right hand if
I have and image which gets stretched to its full background, regardless of the
I have a certain class structure in my app, that currently utilizes django for
In my ASP.NET Web Forms Page I have a ModalPopupExtender that is shown by
For our database development we have on one hand a full schema DDL script,
I am currently working on a hand of cards class that uses enums that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.