Skip to main content

ExpandoObject and Dynamic Programming in c# 4.0

ExpandoObject is a new Object in the .NET Framework that allows you to add and remove properties, methods, and events at runtime just like JavaScript Object. The following example demonstrates how to add two new values and a method in c# and Javascript.
C# Code JavaScript Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;

namespace CSharpDemo
{
    class Program
    {

        static void Main(string[] args)
        {
            dynamic customer = new ExpandoObject();
            customer.City = "Gurgaon";
            customer.Country = "India";
            customer.FullAddress = new Action(() => Console.WriteLine(
                customer.City + " " + customer.Country
                ));
            customer.FullAddress();
            Console.ReadKey();
        }

    }
}
 
        function GetCusomer() {
            var customer = new Object();
            customer.City = "Gurgaon";
            customer.Country = "India";
            customer.FullAddress = function () {
                return this.City + "," + this.Country;
            }
            alert(customer.FullAddress());
        }

Comments

Popular posts from this blog