Core C#

Anonymous Types

This C# program demonstrates how to program in C# with anonymous data types.

Program.cs

using System;

namespace XoaX {
    class Program {
        static void Main(string[] args) {
            // Example 1
            var qHypostaticUnion = new { sNature1 = "Divine", sNature2 = "Human" };
            System.Console.WriteLine(qHypostaticUnion.ToString());

            // Example 2
            var qCylinder = new { dRadius = 5.0, dHeight = 20.0 };
            System.Console.WriteLine(qCylinder.ToString());

            // Example 3
            var qTrinity = new { sName = "God", saPersons =
                new string[] { "Father", "Son", "Holy Spirit" } };
            System.Console.WriteLine(qTrinity.ToString());
            foreach (string sPerson in qTrinity.saPersons) {
                System.Console.WriteLine("   " + sPerson.ToString());
            }

            // Example 4 - an anonymous array of anonymously-type elements
            var qaSacraments = new[] {
                new { sName = "Baptism", sCategory = "Initiation" },
                new { sName = "Eucharist", sCategory = "Initiation" },
                new { sName = "Confirmation", sCategory = "Initiation" },
                new { sName = "Penance", sCategory = "Healing" },
                new { sName = "Anointing of the Sick", sCategory = "Healing" },
                new { sName = "Holy Orders", sCategory = "Service" },
                new { sName = "Matrimony", sCategory = "Service" },};
            System.Console.WriteLine(qaSacraments.ToString());
            foreach (var qSacrament in qaSacraments) {
                System.Console.WriteLine("   " + qSacrament.ToString());
            }
        }
    }
}
 

Output

{ sNature1 = Divine, sNature2 = Human }
{ dRadius = 5, dHeight = 20 }
{ sName = God, saPersons = System.String[] }
   Father
   Son
   Holy Spirit
<>f__AnonymousType5`2[System.String,System.String][]
   { sName = Baptism, sCategory = Initiation }
   { sName = Eucharist, sCategory = Initiation }
   { sName = Confirmation, sCategory = Initiation }
   { sName = Penance, sCategory = Healing }
   { sName = Anointing of the Sick, sCategory = Healing }
   { sName = Holy Orders, sCategory = Service }
   { sName = Matrimony, sCategory = Service }
Press any key to continue . . .
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.