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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:58:19+00:00 2026-05-28T16:58:19+00:00

In C#, an anonymous type can be as follows: method doStuff(){ var myVar =

  • 0

In C#, an anonymous type can be as follows:

method doStuff(){
     var myVar = new {
         a = false, 
         b = true
     }

     if (myVar.a) 
     {
         // Do stuff             
     }
}

However, the following will not compile:

method doStuff(){
     var myVar = new {
         a = false, 
         b = true
     }

     if (myVar.a) 
     {
         myVar.b = true;
     }
}

This is because myVar’s fields are read-only and cannot be assigned to. It seems wanting to do something like the latter is fairly common; perhaps the best solution I’ve seen is to just define a struct outside the method.

However, is there really no other way to make the above block work? The reason it bothers me is, myVar is a local variable of this field, so it seems like it should only be referred to inside the method that uses it. Besides, needing to place the struct outside of the method can make the declaration of an object quite far from its use, especially in a long method.

Put in another way, is there an alternative to anonymous types which will allow me to define a “struct” like this (I realize struct exists in C# and must be defined outside of a method) without making it read-only? If no, is there something fundamentally wrong with wanting to do this, and should I be using a different approach?

  • 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-28T16:58:20+00:00Added an answer on May 28, 2026 at 4:58 pm

    No, you’ll have to create your own class or struct to do this (preferrably a class if you want it to be mutable – mutable structs are horrible).

    If you don’t care about Equals/ToString/GetHashCode implementations, that’s pretty easy:

    public class MyClass {
        public bool Foo { get; set; }
        public bool Bar { get; set; }
    }
    

    (I’d still use properties rather than fields, for various reasons.)

    Personally I usually find myself wanting an immutable type which I can pass between methods etc – I want a named version of the existing anonymous type feature…

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

Sidebar

Related Questions

Possible Duplicate: How can I pass an anonymous type to a method? Im trying
I know I can't write a method like: public var MyMethod() { return new{
How can I write a LINQ query that returns an hierachical anonymous type from
I can't use TryGetValue() from dictionary in linq expression with anonymous type. Dictionary<string, string>
What are the real differences between anonymous type(var) in c# 3.0 and dynamic type(dynamic)
Into some view data i have put the result of an anonymous type: var
I'm attempting to use the following code to serialize an anonymous type to JSON:
When I do a query that returns an anonymous type var assets = from
If I have an anonymous type created by LINQ var ans = from r
I have an anonymous type of this form: new List<MyList>() { new Column {

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.