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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:09:39+00:00 2026-05-28T14:09:39+00:00

An annonymous type can consume memory in two ways. First Way public MyAdd MyFunc()

  • 0

An annonymous type can consume memory in two ways.

First Way

public MyAdd MyFunc()
        {
            return new MyAdd
            {
                name = "Mike",
                Address = "MyTown"
            };
        }


var x = MyFunc();

Second Way

x = new { name = "Mike", Address = "MyTown" };

The difference in these two statement is that the c.name is readonly in case of former and
is modifyable in later case.

My question is, in both cases finally an annonymous type is consuming some value so why in
case of structure it is not read only. Is there any internal mechanism for this?

  • 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-28T14:09:39+00:00Added an answer on May 28, 2026 at 2:09 pm

    No, in your first case you’re not using an anonymous type. The first code is equivalent to:

    var tmp = new MyAdd();
    tmp.name = "Mike";
    tmp.Address = "MyTown";
    return tmp;
    

    No extra type is introduced. I know it looks like the syntax for an anonymous-object-creation-expression or anonymous object initializer (those are the terms the spec uses) but it’s really just using the existing type. This is just an object creation expression using an object initializer – there’s nothing anonymous here.

    See sections 7.6.10.1 and 7.6.10.2 of the C# 4 spec for more information.

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

Sidebar

Related Questions

I'm returning a Json'ed annonymous type: IList<MyClass> listOfStuff = GetListOfStuff(); return Json( new {
I know I can't write a method like: public var MyMethod() { return new{
Can a WCF service return an instance of an anonymous type or do they
I can't cast one anonymous type to another. And I wonder why. public static
The LINQ code returns a anonymous type how can I return a strong type
Can I use an anonymous type as a return type in a Function, and
I wanna create an anonymous type that I can set the property name dynamically.
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>
Possible Duplicate: LINQ to SQL: Return anonymous type? I have a standard LINQ to

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.