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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:30:06+00:00 2026-05-16T12:30:06+00:00

It works like that: >>> def foo(arg): … x = 1 … print locals()

  • 0

It works like that:

>>> def foo(arg): 

...     x = 1
...     print locals()
...     
>>> foo(7)        
{'arg': 7, 'x': 1}
>>> foo('bar')    
{'arg': 'bar', 'x': 1}

And i need something like this in c# .net, or how is it possible to implement it myself?

  • 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-16T12:30:06+00:00Added an answer on May 16, 2026 at 12:30 pm

    This is not possible. The other answers have given you ideas on how you can retrieve the set of local variables and their types, but certainly not their current values at time of execution. There is no provision for this in C# or .NET. The same goes for method parameters: you can retrieve their types (and even their names), but not their values.

    Please allow me to suggest that if you need this, your code design is severely flawed. You should consider rethinking your code structure.

    As a workaround, you could declare a class which has fields instead of the locals you want. Then you can use Reflection to iterate over those fields:

    foreach (var field in typeof(MyClass).GetFields())
        Console.WriteLine("Field name: {0}; field value: {1}",
            field.Name, field.GetValue(myInstance));
    

    However, I think you can easily see that this is not very robust code: any time you make a change to that class, implicit assumptions you made in this code snippet might break.

    The standard, recommended way to pass associative data around in C# is a Dictionary<,>. This is the equivalent to Python’s dict. You can use a Dictionary<string, object> if you need names as keys and arbitrary objects as values, but you will benefit from compile-time validity checks if you use a more specific type than object.

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

Sidebar

Related Questions

I need a class that works like this: >>> a=Foo() >>> b=Foo() >>> c=Foo()
i need a timer that works like this : imagine somebody have a limit
I'm trying to create something that works like this but doesn't have any submenus.
I'm looking for something that works like Lisp's arg-supplied-p variables, to help differentiate a
I've got this code that works: def testTypeSpecialization: String = { class Foo[T] def
We have a reporting solution that works like this. User orders the report to
Until like one hour ago, I was convinced that in python Foo ().bar ()
I'm trying to unit test some code that looks like this: def main(): parser
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
I want to build an app that works like custom URL app where I

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.