¿Qué hay de malo con este fragmento de código?C# Los tipos anónimos no se pueden asignar a - es de solo lectura
class Program
{
static void Main(string[] args)
{
var obj = new { Name = "A", Price = 3.003 };
obj.Name = "asdasd";
obj.Price = 11.00;
Console.WriteLine("Name = {0}\nPrice = {1}", obj.Name, obj.Price);
Console.ReadLine();
}
}
estoy consiguiendo los errores siguientes:
Error 5 Property or indexer 'AnonymousType#1.Name' cannot be assigned to -- it is read only .....\CS_30_features.AnonymousTypes\Program.cs 65 13 CS_30_features.AnonymousTypes
Error 6 Property or indexer 'AnonymousType#1.Price' cannot be assigned to -- it is read only .....\CS_30_features.AnonymousTypes\Program.cs 66 13 CS_30_features.AnonymousTypes
valores Cómo re-establecido en un objeto tipo anónimo?
Una más cosa a destacar, es que si el nuevo tipo anónimo tiene el mismo número y tipo de propiedades en el mismo orden que será de la misma tipo interno como el primero –
Esto podría ser una nota muy útil, ¡gracias por agregar eso! +1 –