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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:18:54+00:00 2026-05-11T04:18:54+00:00

I am not sure when I should use anonymous types instead of local variables

  • 0

I am not sure when I should use anonymous types instead of local variables in C#.

I have:

string  fullMessage // This is the full message including sender and recipient names  string sender = GetMessagePart(fullMessage, 'from'); string recipient = GetMessagePart(fullMessage, 'to');  //do some stuff and deliver the message 

Should I use:

var msg = new {  sender = GetMessagePart(fullMessage, 'from') recipient = GetMessagePart(fullMessage, 'to') }; 

Instead?

  • 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. 2026-05-11T04:18:54+00:00Added an answer on May 11, 2026 at 4:18 am

    Do you mean statically typed variables? Note that anonymous types are statically typed… (removed due to question edit)

    There are 2 problems with C# anonymous types:

    • you can’t expose them through a method API
    • you can’t mutate them (the members are read-only)

    If you only need to know about the data within a single method, and it is read-only, then an anonymous type is handy (and this covers a lot of cases, in reality).

    If you need to mutate the data or pass it out to a caller, then use either a bespoke class, or simple variables (etc).

    In the case given, I can’t see a reason to use an anonymous type; if you just want the values, use the separate variable approach. If a ‘message’ has a defined meaning, declare a Message class and populate that.

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

Sidebar

Related Questions

Seems it is a question about .htaccess , not sure what should I use.
Let's imagine that we have something like: $.post('somescript.php', 'WHAT CAN WE PUT HERE?', function(replyData)
I am having a little difficulty understanding when a person should configure JIRA permissions
I work in a research group and we use the PBS queuing system. I'm
I have the scheduled job which run every 5 seconds. I'm now using backgroudRB
How can I crack two ciphertexts that have used the same key twice? For
When using jQuery's Accordion (in a nested fashion), I want to ensure that when
I am searching for a way to retrieve all emails addresses from a given
I am very confused over something and was wondering if someone could explain. In
I need to write a program to find the mode. Or the most occurrence

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.