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 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

I have a requirement and I am not sure if I should use Analysis
I have the following scenario and I'm not sure if I should use Class
I'm not sure if I should use a FormsCollection or something else, with this
I'm developing a webapp and I'm not sure which pattern design should I use.
Preface : I'm honestly not sure if this should be on StackOverflow, SuperUser or
I'm not 100% sure how I should word this question but I'll try my
I'm not even sure how I should phrase this question. I'm passing some CustomStruct
Just a sidenote : I'm not sure whether I should post this to serverfault
Not quite sure how to get this one. I have a staff table and
Seems it is a question about .htaccess , not sure what should I use.

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.