text

Anonymous Type Differences in C# and VB.NET

Have you Observed anonymous types closely in c# as well as VB.NET? This post covers some interesting differences with the anonymous types in C# and VB.NET.

Check the below sample source code

In VB.NET

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim PersonData As New Dictionary(Of String, Integer)

        PersonData.Add("Ilayathalapathy Vijay", 51)
        PersonData.Add("Ultimate Star Ajithkumar", 49)
        PersonData.Add("SuperStar Rajnikanth", 100)

        Dim MoreThan100MoviesHero = (From m In PersonData
                                 Where m.Value < 50
                                         Select New With
                                                {m.Key, m.Value}).First()

        MoreThan100MoviesHero.Value = 50
        MessageBox.Show(MoreThan100MoviesHero.Key & " has acted in around " & MoreThan100MoviesHero.Value & " Movies")
    End Sub

In C#

 private void Form1_Load(object sender, EventArgs e)
        {
            Dictionary<string, int> PersonData = new Dictionary<string, int>();
            PersonData.Add("Ilayathalapathy Vijay", 51);
            PersonData.Add("Ultimate Star Ajithkumar", 49);
            PersonData.Add("SuperStar Rajnikanth", 100);

            var MoreThan100MoviesHero = (from m in PersonData
                                     where m.Value < 50
                                             select new { m.Key, m.Value }).FirstOrDefault();
            MoreThan100MoviesHero.Value = 200;

        }

Try compiling the VB it should work , but when you try compiling the same c# code it should give you an compiler error

“Property or indexer ‘AnonymousType#1.Value’ cannot be assigned to — it is read only “

Anonymous Type Differences in C# and VB.NET

Interesting isn’t it ?

Anonymous Type Differences in C# and VB.NET

When an anonymous type is used in C#, it is by default created with read-only properties. You can’t assign the values to later after its construction.

But in VB.NET, this is possible 🙂
If you want the Anonymous properties to be read-only in VB.NET, you must include the keyword key in the query like the below example

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim PersonData As New Dictionary(Of String, Integer)

        PersonData.Add("Ilayathalapathy Vijay", 51)
        PersonData.Add("Ultimate Star Ajithkumar", 49)
        PersonData.Add("SuperStar Rajnikanth", 100)

        Dim MoreThan100MoviesHero = (From m In PersonData
                                 Where m.Value < 50
                                         Select New With
                                                {Key m.Key, Key m.Value}).First()

        MoreThan100MoviesHero.Value = 50
        MessageBox.Show(MoreThan100MoviesHero.Key & " has acted in around " & MoreThan100MoviesHero.Value & " Movies")
    End Sub

Leave A Reply

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

You May Also Like

In this post, you’ll learn about the error message “FileOpenAccessDenied – You do not have permissions to open this file...
  • .NET
  • December 3, 2024
You might have had a situation where your code once worked fine in ASP.NET application but now throws the below...
  • .NET
  • December 3, 2024
C# uses the flower bracket “{” and “}” to identify the block or scope of the function or program ....
  • .NET
  • December 3, 2024