Bonaventure Systems's Zoom Meeting 5_30_2025, 8_06_19 AM_Page 1-[1748591224774].png

Advanced C# Features - Complete Deep Dive

This code demonstrates several advanced C# language features that are essential for modern .NET development. Let's explore each concept in detail.

1. Anonymous Types (Extended)

Theory

Anonymous types create compiler-generated classes with read-only properties. The compiler creates different classes based on property names, types, and order.

Key Concepts from the Code

Same vs Different Anonymous Types

// These create the SAME anonymous type class
var v1 = new { No = 100, Name = "Manoj" };    // AnonymousType0<int, string>
var v2 = new { No = 200, Name = "Rahul" };    // Same class

// These create DIFFERENT anonymous type classes
var v3 = new { No = "One", Name = 1234 };     // AnonymousType0<string, int>
var v4 = new { Name = "Manoj", No = 100 };    // AnonymousType1<string, int> (different order)

Anonymous Type Rules

  1. Property order matters - Different order = Different type
  2. Property types matter - Different types = Different type
  3. Property names matter - Different names = Different type
  4. Properties are read-only - Cannot modify after creation

Generated Classes (Conceptual)

// Compiler generates something like:
internal sealed class AnonymousType0<T1, T2> : IEquatable<AnonymousType0<T1, T2>>
{
    public T1 No { get; }
    public T2 Name { get; }

    public AnonymousType0(T1 no, T2 name)
    {
        No = no;
        Name = name;
    }

    public override bool Equals(object obj) { /* implementation */ }
    public override int GetHashCode() { /* implementation */ }
    public override string ToString() { /* implementation */ }
}

2. Anonymous Type Collections

Theory

You can create arrays/collections of anonymous types, but all elements must have the same anonymous type structure.

var arr = new[]
{
    new { No = 1, Name = "Kailash1"},
    new { No = 2, Name = "Kailash2"},
    new { No = 3, Name = "Kailash3"},
    new { No = 4, Name = "Kailash4"},
};

foreach (var item in arr)
{
    Console.WriteLine(item.Name);
}