मुख्य सामग्रीमा जानुहोस्
~/writing

यो लेख अंग्रेजीमा छ।

.NET

Dynamic objects in C#

/ २ मिनेट पढाइ / Ejan Shrestha

n C#, a dynamic object is a type that is resolved at runtime rather than at compile-time. This means that the type of a dynamic object is not known until the code is executed, which allows you to use the object to perform operations that would otherwise not be possible with statically-typed objects.

For example, consider the following code:

codedynamic myDynamicObject = "hello";
myDynamicObject = 123;
myDynamicObject = new { Name = "John", Age = 30 };
 
Console.WriteLine(myDynamicObject.Name);
Console.WriteLine(myDynamicObject.Age);

In this code, we declare a variable called myDynamicObject and assign it a string, an integer, and then an anonymous object. Because myDynamicObject is a dynamic object, we can change its type at runtime.

Later in the code, we try to access the Name and Age properties of myDynamicObject. Because the type of myDynamicObject is not known until runtime, the code will compile without error even though the properties Name and Age are not defined on the first two types we assigned to myDynamicObject.

When the code is executed, however, the third assignment to myDynamicObject creates an anonymous object that has properties called Name and Age, and so the code will output the values of those properties to the console.

Dynamic objects can be useful in certain situations where you need to work with data that has unknown or varying types at runtime. However, because dynamic objects do not provide compile-time type checking, they can also introduce additional runtime errors that would have been caught at compile-time with statically-typed objects. It's important to use dynamic objects judiciously and only when necessary.

How Dynamic Objects Work in C#: An Example

When the myDynamicObject is assigned an anonymous object, the previously assigned string and integer values are lost and are no longer accessible.

This is because myDynamicObject is a dynamic object, which means its type can be changed at runtime. When you assign an anonymous object to myDynamicObject, the type of myDynamicObject changes to the type of the anonymous object, and the previous values assigned to myDynamicObject are no longer available.

So in the code snippet from the previous question, after the line myDynamicObject = new { Name = "John", Age = 30 };, the string and integer values assigned to myDynamicObject are lost and cannot be accessed again. Only the Name and Age properties of the anonymous object assigned to myDynamicObject can be accessed.

Resume.pdf

PDF डाउनलोड

Ejan Shresthaइजन श्रेष्ठ

Senior Software Engineer, backend and .NET modernisation

Senior software engineer in Kathmandu with 4+ years shipping .NET for insurance and enterprise. I own reliability and modernisation for an insurance platform of 50 .NET projects on Kubernetes: structured logging and tracing across old and new services, the plan off end-of-life .NET to .NET 10, and the fix for socket exhaustion across ~390 call sites.

Selected impact

  • ~390Ending socket exhaustion across the platform. The error class disappeared from production logs, and outbound HTTP now has one place for retries and tracing.
  • 1 IDOne trace from gateway to database. One correlation ID now follows a request end to end.
  • 61%From end-of-life .NET to .NET 10. A sequenced roadmap with known risks; the first services already run on .NET 10.
  • v16A public website on Next.js 16. Shipped to production with a tested rollback path.
  • 4A release standard people follow. Releases are predictable enough to schedule.

Experience

  1. Senior Software Engineer, Himalayan Everest Insurance

    Jan 2025 – present

    Kathmandu, hybrid

    • Own backend reliability and modernisation for an insurance platform of 50 .NET projects, deployed as 28 Kubernetes workloads, across accounting, claims, reinsurance and reporting.
    • Leading the upgrade from .NET Core 2.2, 3.1 and 5 to .NET 10, including the IdentityServer4 replacement.
    • Built structured logging and tracing across every service.
    • Upgraded the public website and CMS to Next.js 16, React 19, Strapi 5 and Node 24.
  2. .NET Developer, EKbana Solutions

    Jul 2022 – Nov 2024

    Lalitpur, on-site

    • Designed backend services for enterprise insurance workflows with ASP.NET Core, PostgreSQL, EF Core and Dapper.
    • Built REST APIs and integrations for customer portals, payments and external partners.
    • Ran background workflows on Hangfire with idempotency, retries and failure recovery.
    • Shipped on Docker, Linux, Nginx/Traefik and Kubernetes; worked on IdentityServer, JWT and RBAC.
  3. Dotnet Developer, NBIT

    Jan 2022 – Jun 2022

    Lalitpur

    • Built and maintained .NET Framework applications for client projects.
  4. Data Specialist, CloudFactory

    Nov 2017 – Jun 2022

    Kathmandu

    • Delivered high-accuracy data work for international clients while completing my degree.