MSDEVBUILD - Community of Microsoft AI, Azure and Xamarin by Suthahar - Solution Architect for Microsoft AI, Azure, Xamarin | Tech Author and Speaker
This blog provides an in-depth explanation of object and dynamic types in C#.
object is the base class for all data types in C#.
using System;
class Program
{
static void Main()
{
object obj = 10; // Boxing
int num = (int)obj; // Unboxing
Console.WriteLine(num);
}
}
object stores any data type.dynamic allows runtime type flexibility.
using System;
class Program
{
static void Main()
{
dynamic value = 10;
Console.WriteLine(value);
value = "Hello World";
Console.WriteLine(value);
}
}
dynamic can change its type at runtime.object and dynamic| Feature | object | dynamic |
|---|---|---|
| Type Checking | Compile-time | Runtime |
| Casting Required | Yes | No |
| Performance | Slower due to boxing/unboxing | Faster as it avoids compile-time checks |
| IntelliSense Support | Yes | No |
object and dynamic in C#?A: object requires explicit casting, and type checking is done at compile time, while dynamic skips compile-time checking and determines types at runtime.
dynamic?A:
dynamic over object?A: Use dynamic when working with reflection, COM objects, or dynamic languages like Python.
dynamic affect performance?A: Yes, dynamic operations take longer due to runtime type resolution, whereas object may perform better when using proper casting.
dynamic store primitive types?A: Yes, dynamic can store any type, including primitive types, and allows changing them at runtime.
object when dealing with generic types and need type safety.dynamic when working with unknown types at runtime.dynamic.This guide provides an essential understanding of object and dynamic types, helping developers choose the right type based on their use case.
Microsoft announced a new Foundational C# certification in collaboration with FreeCodeCamp. The Foundational C# certification is completely free for individuals in all regions. The certification includes a 35-hour C# training course hosted on Microsoft Learn and requires completing an 80-question C# certification exam. It's very interesting for those who want to start developing programming skills in C#. This is a great choice, as after completing this certification, you will acquire all the essential C# programming skills and learn tips and tricks.
Without further delay, let's get started, and I will guide you through the certification process step by step.
Create an Account in the Microsoft Learn:
Complete C# Self Learning Course in Microsoft Learn:
Step 3: Complete the training content on Microsoft Learn. (Note: If you have previously completed the training content, you do not need to redo it)
In this article, we will discuss Microsoft introduced new types DateOnly and TimeOnly in C# and examine the unique functionalities offered by each, and assist in determining the suitable choice for different scenarios.
DateOnly and TimeOnly are value types introduced in .Net 6. These types are part of the .net Date and Time and offer unique advantages over using traditional DateTime for specific use cases that require working with only dates or times without the associated time zone information.Use DateOnly when you need to work with dates exclusively, without any time information. Examples of this include handling birthdays, anniversaries, and event dates.
For example, if you were to gather DOB from users, the optimum data type would be Date only as you wouldn't need timezone information and would only need the date, year, and month.Use TimeOnly when you require time values without any date information. Examples include representing opening/closing times, countdowns, and scheduling alarms.
In C#, DateTime is a value type and commonly used in all the programming languages, You should use DateTime when you need to work with both date and time components together or when you require time zone information.
DateTime provides a complete representation of a specific point in time, combining both date and time information along with an optional time zone offset. here you can find some use cases where DateTime is more appropriate than using DateOnly or TimeOnly
Hope this article will help you, the choice between DateTime, DateOnly, and TimeOnly should be driven by the specific needs of your application.
If your application primarily deals with only dates or times, and you don't require time zone support or date and time together, then DateOnly and TimeOnly can be more appropriate for improved code clarity and safety. However, if you need to represent points in time with both date and time information, DateTime is the suitable choice.
Microsoft AI, Azure & Xamarin specialist. Tech Author, Speaker & Community Builder since 2007.