DNX: C#: Optional Parameters

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.

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OptionalParameterExample {
	
	public class OptionalParameterExample {
	
		public OptionalParameterExample(){
			Console.WriteLine("\nOptionalParameterExample");
			
			// DisplayInfo(); // Doesn't work
			DisplayInfo("Alex");
			DisplayInfo("Alex", "Carlstein");
			DisplayInfo("Alex", "Carlstein", age:34);
			DisplayInfo("Alex", age: 35);
			DisplayInfo(lastName: "Carlstein");
			// DisplayInfo(lastName: "Carlstein", 35); // Doesn't work
			DisplayInfo(lastName: "Carlstein", age: 36);	

			
		}
		
		private void DisplayInfo(string firstName = null, string lastName = null, int age = 0){
			Console.WriteLine("{0} {1} is {2} years old.", firstName, lastName, age);
		}
		
	}
	
}
Output:

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

OptionalParameterExample
Alex  is 0 years old.
Alex Carlstein is 0 years old.
Alex Carlstein is 34 years old.
Alex  is 35 years old.
 Carlstein is 0 years old.
 Carlstein is 36 years old.
Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: Optional Parameters," in Alejandro G. Carlstein Ramos Mejia Blog, September 2, 2015, http://www.acarlstein.com/?p=3184.
Share