DNX: C#: Anonymous Types

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
Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: Anonymous Types," in Alejandro G. Carlstein Ramos Mejia Blog, September 1, 2015, http://www.acarlstein.com/?p=3180.
Share

DNX: C#: LINQ

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.

Language Integrated Query (LINQ) 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 LINQExample.LINQExample();
			
		}
		
		private void displayWhichDnxIsCompilingOn(){
			#if DNX451
			Console.WriteLine("Compiled on DNX451: .NET Framework");
			#endif
			
			#if DNXCORE50
			Console.WriteLine("Compiled on DNXCORE50: .NET Core 5");
			#endif						
		}
		
	}
	
}
LINQExample.cs

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQExample {
	
	public class LINQExample {
	
		public LINQExample(){
			Console.WriteLine("\nLINQExample()");
			
			var sample = "This is a text example to show how LINQ works";
			
			var result = from s in sample 
						 select s;
						 
			foreach (var item in result){
				Console.Write(item + "-");
			}
			
			Console.WriteLine("\n------");
			
			result = from s in sample
					 where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					 select s;
					 
			foreach (var item in result){
				Console.Write(item + "-");
			}
			
			Console.WriteLine("\n------");
			
			result = from s in sample.ToLower()
					 where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					 select s;
					 
			foreach (var item in result){
				Console.Write(item + "-");
			}
			
			Console.WriteLine("\n------");
			
			result = from s in sample.ToLower()
					 where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					 orderby s // ascending by default
					 select s;
					 
			foreach (var item in result){
				Console.Write(item + "-");
			}
			
			Console.WriteLine("\n------");

			result = from s in sample.ToLower()
					 where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					 orderby s descending
					 select s;
					 
			foreach (var item in result){
				Console.Write(item + "-");
			}
			
			Console.WriteLine("\n------");

			var result2 = from s in sample.ToLower()
					 	  where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					 	  group s by s;					 
					 
			foreach (var item in result2){
				Console.WriteLine(item);
			}
			
			Console.WriteLine("\n------");

			result2 = from s in sample.ToLower()
					  where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					  group s by s;					 
					 
			foreach (var item in result2){
				Console.Write(item.Key + "-");
			}
			
			Console.WriteLine("\n------");

			result2 = from s in sample.ToLower()
					  where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					  orderby s descending
					  group s by s;					 
					 
			foreach (var item in result2){
				Console.Write(item.Key + "-");
			}
			
			Console.WriteLine("\n------");

			result2 = from s in sample.ToLower()
					  where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					  orderby s 
					  group s by s;					 
					 
			foreach (var item in result2){
				Console.WriteLine(item.Key + ":" + item.Count());
			}
			
			Console.WriteLine("\n------");
			
			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 result3 = from p in people
						  select p;
			
			foreach (var item in result3){
				Console.WriteLine("{0} {1}: {2}", item.FirstName, item.LastName, item.Age);
			}

			Console.WriteLine("\n------");
			
			result3 = from p in people
						  where p.Age < 35
						  select p;
			
			foreach (var item in result3){
				Console.WriteLine("{0} {1}: {2}", item.FirstName, item.LastName, item.Age);
			}

			Console.WriteLine("\n------");
			
			result3 = from p in people
						  where p.Age < 35 && p.LastName == "Carlstein"
						  select p;
			
			foreach (var item in result3){
				Console.WriteLine("{0} {1}: {2}", item.FirstName, item.LastName, item.Age);
			}

			Console.WriteLine("\n------");

			result3 = from p in people
						  orderby p.LastName
						  select p;
			
			foreach (var item in result3){
				Console.WriteLine("{0}, {1}: {2}", item.LastName, item.FirstName, item.Age);
			}

			Console.WriteLine("\n------");

			var result4 = from p in people
						  orderby p.LastName descending
						  group p by p.LastName;
			
			foreach (var item in result4){
				
				Console.WriteLine(item.Key + ": " + item.Count());
				foreach (var p in item){
					Console.WriteLine("\t{0}, {1}", p.LastName, p.FirstName);
				}
				
			}

			Console.WriteLine("\n------");
			
		}
		
		public class Person {
			public string FirstName { get; set; }
			public string LastName { get; set; }
			public int Age { get; set; }
		}
		
	}
		
}
Output:

AGCRM-MacBook-Pro:Examples user$ dnx . me
Main()
Compiled on DNX451: .NET Framework

LINQExample()
T-h-i-s- -i-s- -a- -t-e-x-t- -e-x-a-m-p-l-e- -t-o- -s-h-o-w- -h-o-w- -L-I-N-Q- -w-o-r-k-s-
------
i-i-a-e-e-a-e-o-o-o-o-
------
i-i-a-e-e-a-e-o-o-o-i-o-
------
a-a-e-e-e-i-i-i-o-o-o-o-
------
o-o-o-o-i-i-i-e-e-e-a-a-
------
System.Linq.Grouping`2[System.Char,System.Char]
System.Linq.Grouping`2[System.Char,System.Char]
System.Linq.Grouping`2[System.Char,System.Char]
System.Linq.Grouping`2[System.Char,System.Char]

------
i-a-e-o-
------
o-i-e-a-
------
a:2
e:3
i:3
o:4

------
Alex Carlstein: 33
Diego Carlstein: 32
Alex Veron: 40
Vanina Veron: 34
Bob Smith: 12

------
Alex Carlstein: 33
Diego Carlstein: 32
Vanina Veron: 34
Bob Smith: 12

------
Alex Carlstein: 33
Diego Carlstein: 32

------
Carlstein, Alex: 33
Carlstein, Diego: 32
Smith, Bob: 12
Veron, Alex: 40
Veron, Vanina: 34

------
Veron: 2
	Veron, Alex
	Veron, Vanina
Smith: 1
	Smith, Bob
Carlstein: 2
	Carlstein, Alex
	Carlstein, Diego

------
Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: LINQ," in Alejandro G. Carlstein Ramos Mejia Blog, September 1, 2015, http://www.acarlstein.com/?p=3178.
Share

DNX: C#: Extension Methods

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.

Extension methods 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 ExtensionMethodsExample.ExtensionMethodsExample();
						
		}
		
		private void displayWhichDnxIsCompilingOn(){
			#if DNX451
			Console.WriteLine("Compiled on DNX451: .NET Framework");
			#endif
			
			#if DNXCORE50
			Console.WriteLine("Compiled on DNXCORE50: .NET Core 5");
			#endif						
		}
		
	}
	
}
ExtensionMethodsExamples.cs

using System;

namespace ExtensionMethodsExample {
	
	public class ExtensionMethodsExample {
	
		public ExtensionMethodsExample(){
			Console.WriteLine("\nExtensionMethodsExample()");
			
			var p = new Person { Name = "Pedro", Age = 32 };
			
			p.SayHello();
		}
		
	}
	
	public class Person{
		public string Name;
		public int Age;			
	}
	
	public static class Extensions {
		
		public static void SayHello(this Person person){
			Console.WriteLine("{0} says hello!", person.Name);
		} 
		
	}
	
}
Output:

AGCRM-MacBook-Pro:Examples user$ dnx . me
Main()
Compiled on DNX451: .NET Framework

ExtensionMethodsExample()
Pedro says hello!
Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: Extension Methods," in Alejandro G. Carlstein Ramos Mejia Blog, September 1, 2015, http://www.acarlstein.com/?p=3176.
Share