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

  • Home
  • SEARCH
  • 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 6535337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:21:42+00:00 2026-05-25T10:21:42+00:00

I’m reading some AppHub samples by Microsoft and this is the beginning of one

  • 0

I’m reading some AppHub samples by Microsoft and this is the beginning of one of the functions:

if (string.IsNullOrEmpty(textureFilename))
{
    string message = "textureFilename wasn't set properly, so the " +
        "particle system doesn't know what texture to load. Make " +
        "sure your particle system's InitializeConstants function " +
        "properly sets textureFilename.";
    throw new InvalidOperationException(message);
}

ReSharper says to make this value a constant rather than redeclaring it every time. However, this string value is only used in this function, so making it a member variable should not be necessary. Ideally, the variable’s scope should be limited to this function. Right?

Also, I agree with whoever is about to say “place the string in a resource file.” In this case that would most likely be the optimal solution. Not only does it solve localization issues but it also saves the variable from being re-initialized every function call and no longer clutters up the source file. However, this is just an example.

I know many are probably going to say something along the lines of “premature optimization is the root of all evil” but please note this is just an example. What if this quasi-constant variable was complex and re-initializing it every call would cause a noticeable slowdown?

Visual Basic .NET allows the programmer to declare variables in a function as static. For example, in this code TestFunction will only be called the first time I click the button:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Static example As Integer = TestFunction()

    MessageBox.Show(example)
End Sub

Private Function TestFunction() As Integer
    Console.WriteLine("Method Accessed")

    Return 5
End Function

As far as I know C# does not allow this. Is there any particular reason why? It seems like it would be perfect in this situation. It limits the variables scope to the function and only initializes it the first time around. Even if object creation is costly it will only be performed once. So why is this not available? Or is it?

Thanks for reading!

  • 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-25T10:21:42+00:00Added an answer on May 25, 2026 at 10:21 am

    ReSharper says to make this value a constant rather than redeclaring
    it every time. However, this string value is only used in this
    function, so making it a member variable should not be necessary.
    Ideally, the variable’s scope should be limited to this function.
    Right?

    Yes, but resharper will make the local variable const, not create a member field

    if (string.IsNullOrEmpty(textureFilename))
    {
        const string message = "textureFilename wasn't set properly, so the " +
            "particle system doesn't know what texture to load. Make " +
            "sure your particle system's InitializeConstants function " +
            "properly sets textureFilename.";
        throw new InvalidOperationException(message);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,

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.