in Technology by

What are Anonymous Types in C#?

1 Answer

0 votes
by
Anonymous types allow us to create new types without defining them. This is a way of defining read-only properties in a single object without having to define each type explicitly. Here, Type is generated by the compiler and is accessible only for the current block of code. The type of properties is also inferred by the compiler.
 
We can create anonymous types by using “new” keyword together with the object initializer. 
 
Example 
  1. var anonymousData = new     
  2. {    
  3.     ForeName = "Jignesh",    
  4.     SurName = "Trivedi"    
  5. };    
  6. Console.WriteLine("First Name : " + anonymousData.ForeName);    
Anonymous Types with LINQ Example
 
Anonymous types are also used with the "Select" clause of LINQ query expression to return a subset of properties.
 
Example
 
If any object collection has properties calling FirstName, LastName, DOB, etc... and you want only FirstName and LastName after the Querying the data, then:
  1.     class MyData {    
  2.         public string FirstName {    
  3.             get;    
  4.             set;    
  5.         }    
  6.         public string LastName {    
  7.             get;    
  8.             set;    
  9.         }    
  10.         public DateTime DOB {    
  11.             get;    
  12.             set;    
  13.         }    
  14.         public string MiddleName {    
  15.             get;    
  16.             set;    
  17.         }    
  18.     }    
  19.     static void Main(string[] args) {    
  20.         // Create Dummy Data to fill Collection.    
  21.         List < MyData > data = new List < MyData > ();    
  22.         data.Add(new MyData {    
  23.             FirstName = "RASH", LastName = "BIHTI", MiddleName = "G", DOB = new DateTime(1990, 12, 30)    
  24.         });    
  25.         data.Add(new MyData {    
  26.             FirstName = "VIKI", LastName = "HAYEN", MiddleName = "G", DOB = new DateTime(1995, 11, 6)    
  27.         });    
  28.         data.Add(new MyData {    
  29.             FirstName = "NEWWES", LastName = "TR", MiddleName = "G", DOB = new DateTime(1993, 10, 8)    
  30.         });    
  31.         data.Add(new MyData {    
  32.             FirstName = "Amit", LastName = "Vyas", MiddleName = "P", DOB = newDateTime(1983, 6, 15)    
  33.         });    
  34.         data.Add(new MyData {    
  35.             FirstName = "Yash", LastName = "Pandiya", MiddleName = "K", DOB = newDateTime(1988, 7, 20)    
  36.         });    
  37.     }    
  38.     var anonymousData = from pl in data    
  39.     select new {    
  40.         pl.FirstName, pl.LastName    
  41.     };    
  42.     foreach(var m in anonymousData) {    
  43.         Console.WriteLine("Name : " + m.FirstName + " " + m.LastName);    
  44.     }    
  45. }   

Related questions

0 votes
    What is true about functions : I) Functions are objects II) Can be assigned to a variable III) Can be anonymous IV) Return value type has ... , III 3. I, II, III, IV 4. I, II, IV...
asked Feb 25, 2021 in Technology by JackTerrance
0 votes
    What is true about functions : I) Functions are objects II) Can be assigned to a variable III) Can be anonymous IV) Return value type has ... , III C) I, II, III, IV D) I, II, IV...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    Data are made anonymous by...
asked Mar 1, 2021 in Education by Editorial Staff
0 votes
    Anonymous functions can be created in JavaScript. What do anonymous function do? 1. Overwrite variables that are to ... Automatically define the scope of a value inside a parameter...
asked Feb 25, 2021 in Technology by JackTerrance
0 votes
    A special tool is necessary for entering the network which is _______________ that helps the anonymous internet users to ... ) Mozilla browser 3) Chrome browser 4) Tor browser...
asked Dec 30, 2020 in Technology by JackTerrance
0 votes
    Which of the following is true about the anonymous inner class? a) It has only methods b) Objects can' ... : stackoverflow.com 🔗Source: Java Interview Questions and Answers...
asked Dec 19, 2020 in Technology by Editorial Staff
0 votes
    Anonymous functions can be created in JavaScript. What do anonymous function do? A) Overwrite variables that are to ... Automatically define the scope of a value inside a parameter...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    Given the following F# snippet: type A(children: A list) = member val P1 = "" member val P2 = " ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    In Java you can define a new class inline using anonymous inner classes. This is useful when you need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    In Java you can define a new class inline using anonymous inner classes. This is useful when you need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    In Java you can define a new class inline using anonymous inner classes. This is useful when you need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    In Java you can define a new class inline using anonymous inner classes. This is useful when you need ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    What do you mean by value types and reference types in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    How you can implement nullable types in C#? explain with the syntax of Nullable type....
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    Explain Different Types of Constructors in C#?...
asked Dec 31, 2020 in Technology by JackTerrance
...