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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:09:03+00:00 2026-06-13T10:09:03+00:00

I want immutable anonymous types with named members that can be passed around, compared

  • 0

I want immutable anonymous types with named members that can be passed around, compared and recognized — a merging of Tuples and anonymous types. This doesn’t exist, and I realize that.

So the question is: What is a good idiomatic substitute for this using C#4 or 5?

The use case is fluid LINQ data processing from heterogeneous data sources. In a word, ETL in C#. I do some pretty complex analysis and data comes from multiple platforms and sources. It’s not an option to “just put it all on the same platform and use Entity Framework”. I want to able to fluidly pass around what are essentially arbitrary records — immutable named sets of read only properties.

The only thing I can come up with short of creating a custom immutable POCO for every single otherwise-anonymous type is to use attributes to add compiled annotations to methods which return Tuples. Certainly it wouldn’t be hard to write a code-generator for spitting out the immutable POCOs, but I hate how that clutters up the object model of a project. Using dynamic completely erases all the performance and design-time usefulness of static typing, especially if composing further queries from the results of other methods, so I don’t consider it a viable solution.

// My idea: Adding a attribute to methods to at least record the names
// of the "columns" of a Tuple at a method level
public class NamedTupleAttribute : Attribute {
    public string[] Names { get; private set; }
    public NamedTupleAttribute(string[] Names) { this.Names = Names; }
}

// Using NamedTuple attribute to note meaning of members of Tuple
[NamedTuple(new[] { "StoreNumber", "Gross", "Cost", "Tax" })]
public IEnumerable<Tuple<int, decimal, decimal, decimal>> GetSales { ... }

What I want (Imaginary MSDN documentaion for C# 6):

duck (C# reference)

The duck keyword allows anonymous types to be used in all the statically-typed features of C#. Like normal anonymous types, the compiler will treat anonymous types with the same number, names, and types of properties as having the same type. However, the duck keyword also allows these types to be used in member declarations and as type parameters for generic types.

1. duck type instances

Like anonymous types, instances of duck type objects can only be created using an object initializer
without a type name. The syntax is the same as for a normal anonymous type except that the keyword
duck is added after the new operator:

var record = new duck { StoreNumber=1204,
                        Transaction=410, 
                        Date=new DateTime(2012, 12, 13), 
                        Gross=135.12m, 
                        Cost=97.80m,
                        Tax=12.11m };

2. duck type references

Duck types can be referenced using a duck type literal, a duck type alias, or implicitly when the return type of a property or method can be inferred.

2.1 duck type literals

A duck type can be expressed with a type literal, which can be used in the place of any type reference. Duck type literals consist of the keyword duck followed by a list of name – type identifier pairs just as in a parameter list of a method, except enclosed in curly braces:

// A duck type literal:
duck { int StoreNumber, int Transaction, DateTime Date, decimal Gross, decimal Cost, decimal Tax }

// In a member declaration:
public duck { int StoreNumber, int Transaction, DateTime Date, 
              decimal Gross, decimal Cost, decimal Tax } GetTransaction(...) { ... }

// As a type parameter:
var transactions = new List<duck { int StoreNumber, int Transaction, DateTime Date, 
                                   decimal Gross, decimal Cost, decimal Tax }>();

2.2 duck type aliases

You can assign an alias to a duck type with a using directive immediately after the namespace using directives in a C# code file or namespace. The alias may then be used in the place of any type reference.

// Namespace directives:
using System;
using Odbc = System.Data.Odbc;

// duck type aliases:
using TransTotal = duck { int StoreNumber, int Transaction, DateTime Date, 
                           decimal Gross, decimal Cost, decimal Tax };

// Member declaration:
public TransTotal GetTransaction(...) { ... }

// As a type parameter:
var transactions = new List<TransTotal>();

2.3 Inferred duck types

If the return type of a property or method can be inferred, the body of a duck type literal may be omitted in a member declaration:

// Long form:
public duck { int StoreNumber, int Transaction, DateTime Date, 
              decimal Gross, decimal Cost, decimal Tax } GetDummyTransaction() {
    return new duck { ... };
}

// Short form:
public duck GetDummyTransaction() {
    return new duck { ... };
}

// Short form as a type parameter:
public IEnumerabe<duck> GetTransactions(...) {
    return 
        from record in someProvider.GetDetails(...)
        where ((DateTime)record["Date"]).Date == someDate
        group record by new {
            StoreNumber = (int)record["Number"],
            Transaction = (int)record["TransNum"],
            Date = (DateTime)record["Date"]
        } into transTotal
        select new duck {
            transTotal.Key.StoreNumber,
            transTotal.Key.Transaction,
            transTotal.Key.Date,
            Gross = transTotal.Sum(x => (decimal)x["Gross"]),
            Cost = transTotal.Sum(x => (decimal)x["Cost"]),
            Tax = transTotal.Sum(x => (decimal)x["Tax"]),
        };
}
  • 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-06-13T10:09:04+00:00Added an answer on June 13, 2026 at 10:09 am

    ExpandoObject may be of interest to you.

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

Sidebar

Related Questions

I want to have immutable types that can, ideally, sort out their own hashing
I want to have immutable Java objects like this (strongly simplified): class Immutable {
I want to work with an immutable indexed multidimensional array. The structure that makes
If I want to return an immutable array like this + (NSArray *)ids but
I want to make my collection immutable outside of my class. from this: public:
I want to make sure that a given group of objects is immutable. I
Assume you want to add some methods to all Iterables. That can look like
I want to make sure that I correctly understand the 'Effectively Immutable Objects' behavior
Can we pass a reference of a variable that is immutable as argument in
I want to enforce on my code base immutable rule with following test [TestFixture]

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.