One of the lower known functions of C# is the capability to produce implicit and specific user-defined type conversions, implying we have assistance for both implicit and specific conversions of one type to another type. We likewise have explicit and implicit operators, implying some operators need a specific cast and some operators do not.
This short article talks about these specific and implicit conversion operators and how we can work with them in C#. To work with the code examples offered in this short article, you need to have Visual Studio 2022 set up in your system. If you do not currently have a copy, you can download Visual Studio 2022 here.
Produce a console application task in Visual Studio
First off, let’s develop a.NET Core console application task in Visual Studio. Assuming Visual Studio 2022 is installed in your system, follow the actions laid out listed below to develop a new.NET Core console application project in Visual Studio.
- Introduce the Visual Studio IDE.
- Click “Create brand-new task.”
- In the “Develop new project” window, choose “Console App (. NET Core)” from the list of templates showed.
- Click Next.
- In the “Configure your new task” window revealed next, specify the name and area for the brand-new job.
- Click Next.
- In the “Extra details” window shown next, choose “. NET 7.0 (Standard Term Assistance)” as the framework variation you wish to utilize.
- Click Create.
Following these actions will produce a new.NET Core console application job in Visual Studio 2022. We’ll use this task to deal with type conversions in the subsequent areas of this article.What are implicit and specific type conversions?An implicit type conversion is
one that is done by the runtime instantly. You do not need to cast to any specific type. Here is an example that highlights an implicit conversion: int x= 100; double d =x; Nevertheless,
note that the following code will
not compile. double d= 100.25; int x=d; Here’s the mistake you’ll observe
In Visual Studio on compilation of the above code snippet.< img alt="csharp implicit conversion mistake
1200″height=”543″src=” https://images.idgesg.net/images/article/2021/02/csharp-implicit-conversion-error-100876384-large.jpg?auto=webp&quality=85,70 “/ > IDG Figure 1. The compiler will not let you assign a double to an integer variable in C#.
The mistake suggests that the runtime will not convert a double to an int without specific type casting. This kind of type casting is called explicit type casting since you must compose explicit code to perform the type casting.
You can fix the non-compilable code snippet by defining an explicit type cast of double to int as displayed in the code snippet below.int x = 100; double d = (int) x;
The above code will put together effectively without any errors.Create design and DTO
classes in C# Let’s now comprehend how we can utilize implicit and explicit conversions in user-defined information types, i.e., classes.Consider the following 2 classes. public class Author public class AuthorDto public string Id get; set; public string FirstName get; set; public string LastName get; set;
In the preceding code bit, the Author class is the design, i.e., it represents the Author entity. The AuthorDto class represents the information transfer object of the Author class. An information transfer things is a container of information utilized to pass data between the layers of an application.Convert model to DTO and vice versa in C# The following two methods show how you can convert an Author instance to an AuthorDto circumstances and convert and AuthorDto circumstances to an Author instance.public AuthorDto ConvertAuthorToAuthorDto(Author
author )AuthorDto authorDto=brand-new AuthorDto Id= author.Id.ToString(), FirstName=author.FirstName, LastName = author.LastName; return authorDto; public Author ConvertAuthorDtoToAuthor(AuthorDto authorDto)Author author=brand-new Author ; return author;
If you need to write such conversion code for several classes in your application, you will not only discover it troublesome however also your code will not have proper readability. Here is where implicit and specific conversion operators come in.Use the implicit conversion operator in C#
A better way to achieve the model-DTO conversions showed above is to utilize implicit and explicit operators. When you utilize implicit or explicit conversion operators, you do not need to write cumbersome approaches to convert an instance of one type to another. The code is far more simple.
The following code bit demonstrates how you can make the most of the implicit operator to convert an Author instance to an AuthorDto instance.public static implicit operator AuthorDto(Author author) AuthorDto authorDto = new AuthorDto(); authorDto.Id = author.Id.ToString(); authorDto.FirstName = author.FirstName; authorDto.LastName = author.LastName; return authorDto;
And here’s how you can use the implicit operator to transform an Author circumstances to an AuthorDto instance:
fixed void Main(string  args)
Use the specific conversion operator in C#
The following code snippet shows how you can make the most of the explicit operator to convert an Author circumstances to a circumstances of AuthorDto class.public static
specific operator AuthorDto(Author author) AuthorDto authorDto = new AuthorDto(); authorDto.Id = author.Id.ToString(); authorDto.FirstName = author.FirstName; authorDto.LastName = author.LastName; return authorDto;
In this case you’ll require an explicit cast to convert an Author circumstances to an AuthorDto circumstances as displayed in the code bit provided below.static space Main(string  args)
Note you can not have both implicit and explicit operators specified in a class. If you have actually defined an implicit operator, you will have the ability to transform items both implicitly and explicitly. Nevertheless, if you have actually specified an explicit operator, you will have the ability to convert things clearly only. This discusses why you can not have both implicit and specific operators in a class. Although an implicit cast is more convenient to utilize, an explicit cast offers much better clarity and readability of your code.
Copyright © 2023 IDG Communications, Inc.