As you may know, Microsoft is getting into the open source community with the ASP.NET and DNX with the mono project.
The .NET Execution Environment (DNX) is a software development kit which allows you to run .NET applications on Windows, Mac, and Linux using different frameworks such as .NET framework, .NET Core, and Mono. However, everything are not roses. There are many incomplete libraries, incompatibilities, lack of documentation, and most of the examples such as doing SQL or SOAP do not work depending on which the library targets you are planning to code for. Therefore, I decided to test the basics on all the library targets. I am starting with DNX451.
Anonymous Types seems to be working fine in DNX451 with C#. Below is a code example for those who wish to play with it.
Program.cs
using System; namespace ConsoleApp1 { public class Program { public void Main(string[] args){ Console.WriteLine("Main()"); displayWhichDnxIsCompilingOn(); new AnonymousTypesExample.AnonymousTypesExample(); } private void displayWhichDnxIsCompilingOn(){ #if DNX451 Console.WriteLine("Compiled on DNX451: .NET Framework"); #endif #if DNXCORE50 Console.WriteLine("Compiled on DNXCORE50: .NET Core 5"); #endif } } } |
AnonymousTypesExample.cs
using System; using System.Collections.Generic; using System.Linq; namespace AnonymousTypesExample { public class AnonymousTypesExample { public AnonymousTypesExample(){ Console.WriteLine("\nAnonymousTypesExample()"); var people = new List<Person> { new Person { FirstName = "Alex", LastName = "Carlstein", Age = 33 }, new Person { FirstName = "Diego", LastName = "Carlstein", Age = 32 }, new Person { FirstName = "Alex", LastName = "Veron", Age = 40 }, new Person { FirstName = "Vanina", LastName = "Veron", Age = 34 }, new Person { FirstName = "Bob", LastName = "Smith", Age = 12 }, }; var result = from p in people where p.LastName == "Carlstein" select p; foreach (var item in result){ Console.WriteLine(item.FirstName + " " + item.LastName); } Console.WriteLine("---------"); var result2 = from p in people where p.LastName == "Carlstein" select new { FName = p.FirstName, LName = p.LastName }; foreach (var item in result2){ Console.WriteLine(item.FName + " " + item.LName); } Console.WriteLine("---------"); foreach (var item in result2){ Console.WriteLine(item.FName + " " + item.LName); // item.FName = "Rick"; // Cannot do this because FName is read-only } } } public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public int Dollars { get; set; } public int Pesos { get; set; } public int Yens { get; set; } } } |
Output:
AGCRM-MacBook-Pro:Examples user$ dnx . me Main() Compiled on DNX451: .NET Framework AnonymousTypesExample() Alex Carlstein Diego Carlstein --------- Alex Carlstein Diego Carlstein --------- Alex Carlstein Diego Carlstein |
© 2015, Alejandro G. Carlstein Ramos Mejia. All rights reserved.