Anonymous Types


Anonymous types are used to create an object with a set of read-only properties.

  • The new operator is used to create an anonymous type.
  • Methods and events are not allowed in an anonymous type.
namespace MyConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var jersey = new { Team = "Warriors", Number = 30 };
            Console.WriteLine(jersey.Team + " | " + jersey.Number); //Output: Warriors | 30
        }
    }
}

Back to Notes