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 6934693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:59:36+00:00 2026-05-27T11:59:36+00:00

That is, in C, we can define a function like: func(){ static int foo

  • 0

That is, in C, we can define a function like:

func(){   
  static int foo = 1;   
  foo++;  
  return foo;  
}

and it will return a higher number every time it is called.
Is there an equivalent keyword in C#?

  • 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-27T11:59:37+00:00Added an answer on May 27, 2026 at 11:59 am

    No, there’s no such thing in C#. All state that you want to persist across multiple method calls has to be in fields, either instance or static.

    Except… if you capture the variable in a lambda expression or something like that. For example:

    public Func<int> GetCounter()
    {
        int count = 0;
    
        return () => count++;
    }
    

    Now you can use:

    Func<int> counter = GetCounter();
    Console.WriteLine(counter()); // Prints 0
    Console.WriteLine(counter()); // Prints 1
    Console.WriteLine(counter()); // Prints 2
    Console.WriteLine(counter()); // Prints 3
    

    Now of course you’re only calling GetCounter() once, but that “local variable” is certainly living on well beyond the lifetime you might have expected…

    That may or may not be any use to you – it depends on what you’re doing. But most of the time it really does make sense for an object to have its state in normal fields.

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

Sidebar

Related Questions

i saw a function can be define in javascript like var square = function(number)
What is the maximum number of characters that can be used to define the
I know that when you define a function in vim, you can use the
I have a function that I would like to define in two different Perl
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
Is there a way that I can define __init__ so keywords defined in **kwargs
In Ruby, is there the equivalent of the __str__() method that you can define
How would I define an element that can either contain plain text or contain
In Java you can define generic class that accepts only types that extend a
In C# we can define a generic type that imposes constraints on the types

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.