Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
BRH 48
#DOTNET 34
#ASP.NET 29
jQuery 22
ASP.NET 20
.NET 20
WPF 9
jquery interview questions 9
jquery faq 8
ASP.NET4 8

Archive · View All
February 2011 10
September 2011 4
August 2011 4
July 2011 4
May 2011 4
April 2011 4
March 2011 4
October 2011 4
June 2011 4
January 2011 4

Anonymous Types in C# or VB .NET

Jun 29 2010 5:29PM by Hima   

Introduction

Anonymous types as the name suggests,  help us  to create an object without declaring its data type. Since name of the data type is not specified that the type is referred to as an anonymous type.

We have lot of resources around us on this concept, but I still see some confusion and arguments on this concept that made me to write this blog post. I will try to explain it in simple words so that a novice can understand the basics of the concept.

VB.NET

Emp = New With {
		 .EmplD = 123, 
		 .FName = "Hima", 
		 .LName = “Vejella” , 
		 .Country = "India"
	        }
 

C#.NET

var Emp = new { 
		FirstName = "Hima", 
		LastName = "Vejella", 
		DOJ = DateTime.Now, 
		EMPCode = 150 
	      };
 

The above code snippet is an anonymous declaration in VB. We can notice that the as key word is not required to declare it.

Visual Studio Editor is user-friendly to give us the IntelliSense as below. Once you declared the type then you can access all the properties.

ananymoustypes

When the above code reaches the complier these are the steps that are done internally.

  1. Compiler automatically generates anonymous class.
  2. It anonymously instantiate an object that contains the properties in anonymous type
  3. The instantiated object is assigned to the anonymous type variable "Emp"

The anonymous type inherits from Object, it holds the properties specified in declaring the object.

If we look at the IL Code and we can examine it and understand in detail as explained

anonymous methods

.class private auto ansi sealed VB$AnonymousType_0`4
       extends [mscorlib]System.Object
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() 
   = ( 01 00 00 00 ) 
  .custom instance void [mscorlib]System.Diagnostics.DebuggerDisplayAttribute::.ctor(string)
   = ( 
     01 00 3E 45 6D 70 6C 44 3D 7B 45 6D 70 6C 44 7D   // ..>EmplD={EmplD}
     2C 20 46 4E 61 6D 65 3D 7B 46 4E 61 6D 65 7D 2C   // , FName={FName},
     20 4C 4E 61 6D 65 3D 7B 4C 4E 61 6D 65 7D 2C 20   //  LName={LName}, 
     43 6F 75 6E 74 72 79 3D 7B 43 6F 75 6E 74 72 79   // Country={Country
     7D 00 00 
    )                                        
// }..
} // end of class VB$AnonymousType_0`4

The given code-snippet creates a variable called Emp. This Emp is assigned to an instance of anonymous type that has LastName, FirstName and Country as properties.

To Access Anonymous Type

That is very simple as we access non anonymous types.

VB.NET

MessageBox.Show(Emp.FirstName & " " & Emp.LastName & " " & Emp.Country) 

C#.NET

Console.WriteLine(Emp.FirstName + “Emp.LastName” + ”Emp.DOJ” + “Emp.EmpCode” ); 

This will give us the necessary output

Happy beyond relationaling!!!

Tags: BRH, .NET, C#.NET, VB.NET, Anonymous Types, #DOTNET,


Hima
31 · 6% · 1776
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]