DNX: C#: Dynamic Keyword


Warning: Use of undefined constant setting - assumed 'setting' (this will throw an Error in a future version of PHP) in /homepages/8/d126622285/htdocs/Websites/elblender/wordpress/wp-content/plugins/cite/cite.php on line 105

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.

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

using System;

namespace DynamicKeywordExample {
	
	public class DynamicKeywordExample {
	
		public DynamicKeywordExample(){
			Console.WriteLine("\nDynamicKeywordExample");
			
			var test = "This is a string";
			
			//test = 5; // This will fail because cannot implicitly convert type 'int' to 'string'
			
			Console.WriteLine("Value: {0}", test);
			
			// To be able to change to any time you want use Dynamic DynamicKeywordExample
			
			dynamic test2 = "This is another string";
			
			Console.WriteLine("Value: {0}", test2);
			
			test2 = 10;
			
			Console.WriteLine("Value: {0}", test2);

			// Using Dynamic is bad for performance
			
		}
		
	}
	
}
Output:

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

DynamicKeywordExample
Value: This is a string
Value: This is another string
Value: 10
Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: Dynamic Keyword," in Alejandro G. Carlstein Ramos Mejia Blog, September 2, 2015, http://www.acarlstein.com/?p=3182.

© 2015, Alejandro G. Carlstein Ramos Mejia. All rights reserved.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

*

Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette