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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:36:28+00:00 2026-05-18T05:36:28+00:00

One of the common programming best practices is "define variables as close to where

  • 0

One of the common programming best practices is "define variables as close to where they are used as possible".

I use structs frequently to create code thats almost self documenting in places. However, C# forces me to define the struct outside the method. This breaks the aforementioned best practice – its basically creating an unwanted global variable type for the entire class.

Is it possible to define a local struct inside a method, just like a local variable, and if not, could you give me a window into the reasons the C# designers decided to prevent this?

Use Case

I’m converting part of a spreadsheet into C# code. I’d like to use local structs within the method to store temporary information in an organized manner, without having to resort to hundreds of separate variables that are global in scope.


Update 2016-August: C# 7.0 may have this feature!

As of 2016-Aug, apparently, this will be a feature in C# 7.0.

So the C# compiler team agreed – wow!


Update 2020-July: Now supported by C# and C++

C++ has always fully supported this. And it’s fantastic.

C# 7.0 now has value tuples for a lightweight data structure with named fields. See answer from Ghost4Man.

  • 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-18T05:36:28+00:00Added an answer on May 18, 2026 at 5:36 am

    I believe it’s not permitted to define named types within a method. As to why, I’ll have to speculate. If a type is not going to be used outside, then its existence probably cannot be justified.

    You can however define anonymous type variables within a method. It will somewhat resembles structures. A compromise.

    public void SomeMethod ()
    {
        var anonymousTypeVar = new { x = 5, y = 10 };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably one of the most common tasks / problems when programming; You
Ever since switching from TABLE-layout to DIV-layout, one common problem remains: PROBLEM : you
Does anyone know of papers/books/etc. that document patterns for databases? For example, one common
I have a database where one of the common queries is has a where
One of the most common dilemmas I have when commenting code is how to
Fragile base class is one of the most common point that gets popped up
There's a common way to store multiple values in one variable, by using a
We are factoring out the common code from our Perl project. One main program
When writing multithreaded applications, one of the most common problems experienced is race conditions.
When writing multi-threaded applications, one of the most common problems experienced are deadlocks. My

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.