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://blog.acarlstein.com/?p=3176.
Share
Leave a comment

DNX: C#: Events

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.

Events 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 EventsExample1.EventsExample();
			
			new EventsExample2.EventsExample2();
			
			new EventsExample3.EventsExample3();

		}
		
		private void displayWhichDnxIsCompilingOn(){
			#if DNX451
			Console.WriteLine("Compiled on DNX451: .NET Framework");
			#endif
			
			#if DNXCORE50
			Console.WriteLine("Compiled on DNXCORE50: .NET Core 5");
			#endif						
		}
		
	}
	
}
EventsExample1.cs

using System;

namespace EventsExample1 {
	
	public class EventsExample {
	
		public EventsExample(){
			Console.WriteLine("\nEventsExample()");
			
			var alarmClock = new AlarmClock();
			var alex = new Person("Alex", alarmClock);
			
			alarmClock.AlarmAtSixAM();
			
			Console.WriteLine("-----");
			
			var diego = new Person("Diego", alarmClock);
			
			alarmClock.AlarmAtFivePM();
			
			// Go to EventsExamples2 to continue tutorial
		}
		
	}
	
	public class Person {
		private string _name;
		private AlarmClock _alarmClock;
		public Person(string name, AlarmClock alarmClock){
			_name = name;
			_alarmClock = alarmClock;
			//_alarmClock.Alarm += WakeUp
			_alarmClock.Alarm += () => { Console.WriteLine("\t{0} is waking up", _name); };
		}
		
		//private WakeUp(){
		//	Console.WriteLine("{0} is waking up", _name);
		//}
		
	}
	
	public delegate void AlarmEventHandler();
	public class AlarmClock{
		
		public event AlarmEventHandler Alarm;
		
		public void AlarmAtFivePM(){
			Alarm();
		}
		
		public void AlarmAtSixAM(){
			Alarm();
		}
	}

}
EventsExample2.cs

using System;

namespace EventsExample2 {
	
	public class EventsExample2 {
	
		public EventsExample2(){
			Console.WriteLine("\nEventsExample2()");
			
			var alarmClock = new AlarmClock();
			var alex = new PersonToWakeUp("Alex", alarmClock);
			
			alarmClock.AlarmAtSixAM();
			
			Console.WriteLine("-----");
			
			var diego = new PersonToWakeUp("Diego", alarmClock);
			
			alarmClock.AlarmAtFivePM();
				
			// Go to EventsExamples3 to continue tutorial	
			
			Console.WriteLine("-----");	
		}
		
	}
	
	public class PersonToWakeUp {
		private string _name;
		private AlarmClock _alarmClock;
		
		public PersonToWakeUp(string name, AlarmClock alarmClock){
			_name = name;
			_alarmClock = alarmClock;
			_alarmClock.Alarm += (object sender, EventArgs args) => Console.WriteLine("\t{0} is waking up", _name); 
		}
				
	}
	
	public delegate void AlarmEventHandler(object send, EventArgs args);
	public class AlarmClock{
		
		public event AlarmEventHandler Alarm;
		
		public void AlarmAtFivePM(){
			Alarm(this, EventArgs.Empty);
		}
		
		public void AlarmAtSixAM(){
			Alarm(this, EventArgs.Empty);
		}
	}

}
EventsExamples3.cs

using System;

namespace EventsExample3 {
	
	public class EventsExample3 {
	
		public EventsExample3(){
			Console.WriteLine("\nEventsExample3()");
			
			var alarmClock = new AlarmClock();
			var alex = new PersonToWakeUp("Alejandro", alarmClock);
			var vanina = new PersonToWakeUp("Vanina", alarmClock);
			
			alarmClock.AlarmAtSixAM();
						
			alarmClock.AlarmAtFivePM();
				
		}
		
	}
		
	public class PersonToWakeUp {
		private string _name;
		private AlarmClock _alarmClock;
		
		public PersonToWakeUp(string name, AlarmClock alarmClock){
			_name = name;
			_alarmClock = alarmClock;
			_alarmClock.Alarm += (object sender, AlarmClockEventArgs args) => {
				
				switch (args.Time){
					case 6:
						Console.WriteLine("\t{0} is waking up and going to work.", _name); 
						break;					
					case 17:
						Console.WriteLine("\t{0} stop working and going home.", _name);
						break;
					default:
						break;
				}
			};
			 
		}
				
	}
	
	public delegate void AlarmEventHandler(object send, AlarmClockEventArgs args);
	public class AlarmClock{
		
		public event AlarmEventHandler Alarm;
		
		public void AlarmAtFivePM(){
			Alarm(this, new AlarmClockEventArgs { Time = 17 });
		}
		
		public void AlarmAtSixAM(){
			Alarm(this, new AlarmClockEventArgs { Time = 6 });
		}
	}
	
	public class AlarmClockEventArgs : EventArgs{
		public int Time{ get; set; }
	}


}
Output:

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

EventsExample()
	Alex is waking up
-----
	Alex is waking up
	Diego is waking up

EventsExample2()
	Alex is waking up
-----
	Alex is waking up
	Diego is waking up
-----

EventsExample3()
	Alejandro is waking up and going to work.
	Vanina is waking up and going to work.
	Alejandro stop working and going home.
	Vanina stop working and going home.
Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: Events," in Alejandro G. Carlstein Ramos Mejia Blog, September 1, 2015, http://blog.acarlstein.com/?p=3174.
Share
Leave a comment

DNX: C#: Lambda

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.

Lambda 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 LambdaExample();			

		}
		
		private void displayWhichDnxIsCompilingOn(){
			#if DNX451
			Console.WriteLine("Compiled on DNX451: .NET Framework");
			#endif
			
			#if DNXCORE50
			Console.WriteLine("Compiled on DNXCORE50: .NET Core 5");
			#endif						
		}
		
	}
	
}
LambdaExample.cs

using System;

namespace ConsoleApp1 {

	public class LambdaExample {

		delegate void Operation(int num);
		delegate int ReturnOperation(int num);

		public LambdaExample(){
			Console.WriteLine("\nLambdaExample()");
		
			// Using Anonymous Methods
			Operation opSquare = delegate(int number) { 
				Console.WriteLine("{0} x {0} = {1}", number, number * number); 
			};
			opSquare(2);
			
			Console.WriteLine("------");
						
			ReturnOperation returnOpSquare = delegate(int number){
				return number * number;
			};			
			Console.WriteLine("{0} x {0} = {1}", 2, returnOpSquare(2));

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

			// Doing something similar but using Lambdas:
			Operation op = (int number) => { 
				Console.WriteLine("{0} x {0} = {1}", number, number * number);				
			};
			op(2);
			
			Operation op2 = number => { 
				Console.WriteLine("{0} x {0} = {1}", number, number * number);				
			};
			op2(2);
			
			Console.WriteLine("------");
					
		}		
					
	}
	
}
Output:

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

LambdaExample()
2 x 2 = 4
------
2 x 2 = 4
------
2 x 2 = 4
2 x 2 = 4
------
Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: Lambda," in Alejandro G. Carlstein Ramos Mejia Blog, September 1, 2015, http://blog.acarlstein.com/?p=3165.
Share
Leave a comment

DNX: C#: Anonymous 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.

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

	}
	
}
AnonymousMethodExample.cs

using System;

namespace ConsoleApp1 {
	
	public class AnonymousMethodExample {

		delegate void Operation(int num);
		delegate int ReturnOperation(int num);
	
		public AnonymousMethodExample(){
			Console.WriteLine("\nAnonymousMethodExample()");
		
			Operation opSquare = delegate(int number) { 
				Console.WriteLine("{0} x {0} = {1}", number, number * number); 
			};
			opSquare(2);
			
			Console.WriteLine("------");
						
			ReturnOperation returnOpSquare = delegate(int number){
				return number * number;
			};			
			Console.WriteLine("{0} x {0} = {1}", 2, returnOpSquare(2));
			
		}		
		
	}
	
}
 Output:

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

AnonymousMethodExample()
2 x 2 = 4
------
2 x 2 = 4


Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: Anonymous Methods," in Alejandro G. Carlstein Ramos Mejia Blog, September 1, 2015, http://blog.acarlstein.com/?p=3161.
Share
Leave a comment

DNX: C#: Delegate, Func and Action

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.

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

using System;

namespace ConsoleApp1 {

	public class DelegateExample {
		
		delegate void SomeDelegate();
		
		delegate void Operation(int number);
		
		public DelegateExample() {
			Console.WriteLine("\nDelegateExample()");
			
			// Calling directly
			SampleMethod();
			
			// Calling using delegate 
			SomeDelegate del = new SomeDelegate(SampleMethod);
			del.Invoke();
			
			// Another way to use delegate
			SomeDelegate del2 = SampleMethod;
			del2();
			
			// Calling delegate within method
			CallingDelegate(del2);
			
			// Getting a delegate
			del2 = GetDelegate();
			CallingDelegate(del2);

			Console.WriteLine("----");
						
			// Displaying Values						
			Operation op = DisplaySquare;
			ExecuteOperation(2, op);
			
			op = DisplayCube;
			ExecuteOperation(2, op);
			
			Console.WriteLine("----");
			
			// Add Methods to execute
			op = DisplaySquare;
			op = op + DisplayCube;
			ExecuteOperation(2, op);
			
			Console.WriteLine("----");
						
			// Add and substract methods to execute
			op = DisplaySquare;
			op += DisplayCube;
			op += DisplayCube;
			op += DisplayCube;
			ExecuteOperation(2, op);
			
			Console.WriteLine("----");
						
			// Instead of displaying square once and cube three times, we wish to do square once and cube double.
			// Lets remove one
			op -= DisplayCube;			
			ExecuteOperation(2, op);
			
			Console.WriteLine("----");
							
			// Action is like delegates for functions that doesn't return something.
			Action<int> actionOp = number => { 
				Console.WriteLine("{0} x {0} x {0} = {1}", number, number * number * number);				
			};
			actionOp(2);
			
			Console.WriteLine("------");
			
			// Action is like delegate for function that accept parameters and have a return value.
			Func<int, int> funcOp = number => { return number * number * number * number; };
			Console.WriteLine("{0} x {0} x {0} x {0} = {1}", 2, funcOp(2));
								
		}
		
		private void SampleMethod(){
			Console.WriteLine("\tSampleMethod()");
		}
				
		private void CallingDelegate(SomeDelegate sDel){
			sDel();
		}
		
		private SomeDelegate GetDelegate(){
			return new SomeDelegate(SampleMethod);
		}
		
		private void DisplaySquare(int number){
			Console.WriteLine("{0} x {0} = {1}", number, number * number);
		} 

		private void ExecuteOperation(int num, Operation operation){
			operation(num);			
		}
		
		private void DisplayCube(int number){
			Console.WriteLine("{0} x {0} x {0} = {1}", number, number * number * number);
		}
			
	}

}
Output:

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

DelegateExample()
    SampleMethod()
    SampleMethod()
    SampleMethod()
    SampleMethod()
    SampleMethod()
----
2 x 2 = 4
2 x 2 x 2 = 8
----
2 x 2 = 4
2 x 2 x 2 = 8
----
2 x 2 = 4
2 x 2 x 2 = 8
2 x 2 x 2 = 8
2 x 2 x 2 = 8
----
2 x 2 = 4
2 x 2 x 2 = 8
2 x 2 x 2 = 8
----
2 x 2 x 2 = 8
------
2 x 2 x 2 x 2 = 16
Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: Delegate, Func and Action," in Alejandro G. Carlstein Ramos Mejia Blog, September 1, 2015, http://blog.acarlstein.com/?p=3158.
Share
Leave a comment