Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Setting values for anonymous types

Setting values for anonymous types

Scheduled Pinned Locked Moved C#
helptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yoyosch
    wrote on last edited by
    #1

    Suppose we got the following code snippet: var a = new {i=1, j=2}; How to assign a value for the specific field of that variable? I mean, something like this: a.i = 5; thanks for help

    C 1 Reply Last reply
    0
    • Y Yoyosch

      Suppose we got the following code snippet: var a = new {i=1, j=2}; How to assign a value for the specific field of that variable? I mean, something like this: a.i = 5; thanks for help

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Yoyosch wrote:

      How to assign a value for the specific field of that variable?

      You can't. When you create an anonymous type the following code is generated:

      private static void Main(string[] args)
      {
      var a;
      a = new <>f__AnonymousType0<int, int>(1, 2);
      return;
      }

      The special class <>f_AnonymousType0 looks like this:

      [CompilerGenerated, DebuggerDisplay(@"\{ i = {i}, j = {j} }", Type="<Anonymous Type>")]
      internal sealed class <>f__AnonymousType0<<i>j__TPar, <j>j__TPar>
      {
      // Fields
      [DebuggerBrowsable(0)]
      private readonly <i>j__TPar <i>i__Field;
      [DebuggerBrowsable(0)]
      private readonly <j>j__TPar <j>i__Field;

      // Methods
      \[DebuggerHidden\]
      public <>f\_\_AnonymousType0(<i>j\_\_TPar i, <j>j\_\_TPar j);
      \[DebuggerHidden\]
      public override bool Equals(object value);
      \[DebuggerHidden\]
      public override int GetHashCode();
      \[DebuggerHidden\]
      public override string ToString();
      
      // Properties
      public <i>j\_\_TPar i { get; }
      public <j>j\_\_TPar j { get; }
      

      }

      As you can see, there are no setters on any of those properties.

      Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups