var v = new { Amount = 108, Message = "Hello" };
var name = "Scott";
is identical with
String name = "Scott";
var name; // will generate error
var name = "Scott"; // will compile clean
var v = new { Amount = 108, Message = "Hello" };
the usage of the
new keyword. It is used without a type name. What follows the
new keyword is the
object initializer mentioned previously. It is its duty to create a class for us with declared properties.
class __NONAME__
{
public int Amount { get; set; }
public string Message { get; set; }
}
// Program.cs
using System;
using System.Query;
using System.Data.DLinq;
namespace AnonTypes
{
class Program
{
static void Main(string[] args)
{
var p1 = new {Name = "A", Price = 3}; /* creates new anonymous type with String value A for property Name and int value 3 for property Price */
Console.WriteLine("Name = {0}\nPrice = {1}",
p1.Name, p1.Price);
Console.ReadLine();
}
}
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)