One of many lesser recognized options of C# is the power to create implicit and specific user-defined kind conversions, which means we have now assist for each implicit and specific conversions of 1 kind to a different kind. We even have specific and implicit operators, which means some operators require an specific solid and a few operators don’t.
This text talks about these specific and implicit conversion operators and the way we will work with them in C#. To work with the code examples supplied on this article, it’s best to have Visible Studio 2022 put in in your system. Should you don’t have already got a replica, you may obtain Visible Studio 2022 right here.
Create a console software undertaking in Visible Studio
First off, let’s create a .NET Core console software undertaking in Visible Studio. Assuming Visible Studio 2022 is put in in your system, comply with the steps outlined under to create a brand new .NET Core console software undertaking in Visible Studio.
- Launch the Visible Studio IDE.
- Click on on “Create new undertaking.”
- Within the “Create new undertaking” window, choose “Console App (.NET Core)” from the listing of templates displayed.
- Click on Subsequent.
- Within the “Configure your new undertaking” window proven subsequent, specify the title and placement for the brand new undertaking.
- Click on Subsequent.
- Within the “Extra info” window proven subsequent, select “.NET 7.0 (Normal Time period Assist)” because the framework model you need to use.
- Click on Create.
Following these steps will create a brand new .NET Core console software undertaking in Visible Studio 2022. We’ll use this undertaking to work with kind conversions within the subsequent sections of this text.
What are implicit and specific kind conversions?
An implicit kind conversion is one that’s completed by the runtime routinely. You don’t must solid to any particular kind. Right here is an instance that illustrates an implicit conversion:
int x = 100;
double d = x;
Nevertheless, word that the next code is not going to compile.
double d = 100.25;
int x = d;
Right here’s the error you’ll observe In Visible Studio on compilation of the above code snippet.
The error signifies that the runtime is not going to convert a double to an int with out specific kind casting. Any such kind casting is named specific kind casting since you should write specific code to carry out the sort casting.
You may repair the non-compilable code snippet by specifying an specific kind solid of double to int as proven within the code snippet under.
int x = 100;
double d = (int) x;
The above code will compile efficiently with none errors.
Create mannequin and DTO lessons in C#
Let’s now perceive how we will use implicit and specific conversions in user-defined information sorts, i.e., lessons.
Take into account the next two lessons.
public class Creator
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class AuthorDto
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Within the previous code snippet, the Creator class is the mannequin, i.e., it represents the Creator entity. The AuthorDto class represents the information switch object of the Creator class. A knowledge switch object is a container of information used to cross information between the layers of an software.
Convert mannequin to DTO and vice versa in C#
The next two strategies present how one can convert an Creator occasion to an AuthorDto occasion and convert and AuthorDto occasion to an Creator occasion.
public AuthorDto ConvertAuthorToAuthorDto(Creator creator)
{
AuthorDto authorDto = new AuthorDto
{
Id = creator.Id.ToString(),
FirstName = creator.FirstName,
LastName = creator.LastName
};
return authorDto;
}
public Creator ConvertAuthorDtoToAuthor(AuthorDto authorDto)
{
Creator creator = new Creator
{
Id = Guid.Parse(authorDto.Id),
FirstName = authorDto.FirstName,
LastName = authorDto.LastName
};
return creator;
}
If it’s worthwhile to write such conversion code for a number of lessons in your software, you’ll not solely discover it cumbersome but additionally your code is not going to have correct readability. Right here is the place implicit and specific conversion operators are available in.
Use the implicit conversion operator in C#
A greater technique to obtain the model-DTO conversions illustrated above is to make use of implicit and specific operators. While you use implicit or specific conversion operators, you don’t have to write down cumbersome strategies to transform an occasion of 1 kind to a different. The code is rather more easy.
The next code snippet exhibits how one can reap the benefits of the implicit operator to transform an Creator occasion to an AuthorDto occasion.
public static implicit operator AuthorDto(Creator creator)
{
AuthorDto authorDto = new AuthorDto();
authorDto.Id = creator.Id.ToString();
authorDto.FirstName = creator.FirstName;
authorDto.LastName = creator.LastName;
return authorDto;
}
And this is how you should utilize the implicit operator to transform an Creator occasion to an AuthorDto occasion:
static void Principal(string[] args)
{
Creator creator = new Creator();
creator.Id = Guid.NewGuid();
creator.FirstName = "Joydip";
creator.LastName = "Kanjilal";
AuthorDto authorDto = creator;
Console.ReadKey();
}
Use the specific conversion operator in C#
The next code snippet exhibits how one can reap the benefits of the specific operator to transform an Creator occasion to an occasion of AuthorDto class.
public static specific operator AuthorDto(Creator creator)
{
AuthorDto authorDto = new AuthorDto();
authorDto.Id = creator.Id.ToString();
authorDto.FirstName = creator.FirstName;
authorDto.LastName = creator.LastName;
return authorDto;
}
On this case you’ll want an specific solid to transform an Creator occasion to an AuthorDto occasion as proven within the code snippet given under.
static void Principal(string[] args)
{
Creator creator = new Creator();
creator.Id = Guid.NewGuid();
creator.FirstName = "Joydip";
creator.LastName = "Kanjilal";
AuthorDto authorDto = (AuthorDto)creator;
Console.ReadKey();
}
Be aware you can’t have each implicit and specific operators outlined in a category. When you’ve got outlined an implicit operator, it is possible for you to to transform objects each implicitly and explicitly. Nevertheless, when you have outlined an specific operator, it is possible for you to to transform objects explicitly solely. This explains why you can’t have each implicit and specific operators in a category. Though an implicit solid is extra handy to make use of, an specific solid supplies higher readability and readability of your code.
Copyright © 2023 IDG Communications, Inc.