anonymous class

C# allows anonymous classes in which a class can be declared inside main and initialized without specifying the data types of the class variables. On compilation, the following variable “captain” will be converted into a class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace ConsoleApplication6
    class Program

        static void Main(string[] args)
 var captain = new { FirstName = "Ali", MiddleName = "Khan", LastName = "Shah", Age = 40 };

            Console.WriteLine("{0}, {1}, {2}", captain.FirstName, captain.MiddleName, captain.LastName);

            var team = new { captain.FirstName, captain.MiddleName, captain.LastName };

            Console.WriteLine("{0}, {1}, {2}",team.FirstName, team.MiddleName, team.LastName);


In above class, it is mandatory to assign value in the variables FirstName, MiddleName, and LastName

Add a Comment

Your email address will not be published. Required fields are marked *