11 Oct, 2021
Categories: Advanced C#
Run Application
using System;
using System.Collections.Generic;
using System.Dynamic;
namespace LearnAdvancedCSharpWithRealApps
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Demo 1");
Console.WriteLine("Add: " + Add(1, 2));
Console.WriteLine("\nDemo 2");
foreach(dynamic name in FindAll1())
{
Console.WriteLine(name);
}
Console.WriteLine("\nDemo 3");
foreach (dynamic name in FindAll2())
{
Console.WriteLine(name);
}
Console.WriteLine("\nDemo 4");
Print(Find());
Console.ReadLine();
}
static dynamic Add(dynamic number1, double number2)
{
return number1 + number2;
}
static dynamic FindAll1()
{
return new List<dynamic> {
"Name 1", "Name 2", "Name 3", "Name 4"
};
}
static dynamic FindAll2()
{
return new dynamic[] {
"Name 1", "Name 2", "Name 3", "Name 4"
};
}
static dynamic Find()
{
dynamic student = new ExpandoObject();
student.Id = "st0";
student.Name = "Name 1";
student.Age = 20;
student.Score = 5.6;
student.Status = true;
student.Birthday = DateTime.Now;
student.Address = new ExpandoObject();
student.Address.Street = "Street 1";
student.Address.Ward = "Ward 1";
student.Address.District = "District 1";
dynamic course1 = new ExpandoObject();
course1.Id = "c1";
course1.Name = "Course 1";
dynamic course2 = new ExpandoObject();
course2.Id = "c2";
course2.Name = "Course 2";
dynamic course3 = new ExpandoObject();
course3.Id = "c3";
course3.Name = "Course 3";
student.Courses = new List<dynamic> {
course1, course2, course3
};
return student;
}
static void Print(dynamic student)
{
Console.WriteLine("Student Info");
Console.WriteLine("id: " + student.Id);
Console.WriteLine("name: " + student.Name);
Console.WriteLine("age: " + student.Age);
Console.WriteLine("score: " + student.Score);
Console.WriteLine("status: " + student.Status);
Console.WriteLine("birthday: " + student.Birthday.ToString("dd/MM/yyyy"));
Console.WriteLine("Address: ");
Console.WriteLine("\tstreet: " + student.Address.Street);
Console.WriteLine("\tward: " + student.Address.Ward);
Console.WriteLine("\tdistrict: " + student.Address.District);
Console.WriteLine("Courses: ");
foreach (dynamic course in student.Courses)
{
Console.WriteLine("\tcourse id: " + course.Id);
Console.WriteLine("\tcourse name: " + course.Name);
Console.WriteLine("\t----------------------");
}
}
}
}
Output
Demo 1
Add: 3
Demo 2
Name 1
Name 2
Name 3
Name 4
Demo 3
Name 1
Name 2
Name 3
Name 4
Demo 4
Student Info
id: st0
name: Name 1
age: 20
score: 5.6
status: True
birthday: 11/10/2021
Address:
street: Street 1
ward: Ward 1
district: District 1
Courses:
course id: c1
course name: Course 1
----------------------
course id: c2
course name: Course 2
----------------------
course id: c3
course name: Course 3
----------------------
Tags
Building C# objects dynamically with ExpandoObjectc#C# dynamic Typescreate an object dynamically in c#Create Dynamic Object with Dynamic Type in C#Create objects dynamically in C#create own dynamic type or dynamic object in C#Creating and Using Dynamic ObjectsCreating Dynamic Object With Dynamic Feature in C#Dynamic Type in C#Object and Dynamic Array in C#Use Method Parameters with Dynamic Type in C#Working with the Dynamic Type in C#