TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# explicit and implicit Keywords

Use the explicit and implicit keywords to implement conversions with operators.
Explicit, implicit. Explicit provides conversion functionality. An explicit conversion involves casting from one type to another.
With the explicit keyword, we implement the casting functionality as an operator method. This keyword (along with implicit) is used in operator overloading.Operator
Example, explicit. This program shows 2 classes. Each provides a public static explicit operator: the Apartment provides a House operator, and the House provides an Apartment operator.

Here: These explicit operators are implemented by constructing a new instance of the target type.

And: They set the Name property. Thus the Apartment or House is now of the opposite type but has the same data field.

Info: When you use a cast like (Apartment) or (House), this is an explicit cast. An implicit cast never uses this syntax.

Casts
C# program that uses explicit keyword using System; class Apartment { public string Name { get; set; } public static explicit operator House(Apartment a) { return new House() { Name = a.Name }; } } class House { public string Name { get; set; } public static explicit operator Apartment(House h) { return new Apartment() { Name = h.Name }; } } class Program { static void Main() { House h = new House(); h.Name = "Broadway"; // Cast a House to an Apartment. Apartment a = (Apartment)h; // Apartment was converted from House. Console.WriteLine(a.Name); } } Output Broadway
Example, implicit. With implicit, we allow a conversion from one class to another without any syntax. It is possible to assign one class instance to another. No cast expressions are needed.

Tip: Implicit requires a public static method that returns the type you want to convert to and accepts the type you are converting from.

Next: We provide a Machine class and a Widget class. Both classes have implicit conversion operators.

Operators: The implicit operators here convert the Machine and Widget types by manipulating their _value fields.

And: The Widget and Machine may be conceptually equal but have a different representation of their data.

C# program that uses implicit operator using System; class Machine { public int _value; public static implicit operator Widget(Machine m) { Widget w = new Widget(); w._value = m._value * 2; return w; } } class Widget { public int _value; public static implicit operator Machine(Widget w) { Machine m = new Machine(); m._value = w._value / 2; return m; } } class Program { static void Main() { Machine m = new Machine(); m._value = 5; Console.WriteLine(m._value); // Implicit conversion from machine to widget. Widget w = m; Console.WriteLine(w._value); // Implicit conversion from widget to machine. Machine m2 = w; Console.WriteLine(m2._value); } } Output 5 10 5
Discussion. What is the point of explicit? It provides a special syntax form for converting types. This can be more intuitive for certain operations, mainly ones that involve numeric types.

However: For classes such as Apartment or House, an explicit operator is not normally needed or useful.

And: The example here is for illustrative purposes. Explicit should be rarely used.

Discussion, implicit. You should only use the implicit operator if you are developing a commonly used type. It is probably most useful for the .NET Framework itself.

Thus: Implicit is not something that most programs will require. It is worth knowing it exists, but not often useful.

A summary. As with implicit, the explicit keyword is used to implement conversions. You should be careful with implementing conversions so that they are reversible and make sense.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf