SUNWEN tutorial - C# advanced (9)
Now what I want to talk about is user-defined conversions (User-Defined Conversions) in C#, which uses the knowledge of struct mentioned earlier, which is the structure. Have you forgotten? Okay, just don’t forget. Let’s start with the following In the course, we can see the use of structure (I was still thinking about its use just now, haha). What is declared with class is a class, and what is declared with struct can be regarded as a type, yes, it is like the one that comes with C# Types like int, short, long.
C# allows us to convert structures (structs) and classes (classes), so we can define some conversions in them. However, C# stipulates that all conversion declarations must Choose one between explicit and implicit. For example, when we use this statement
int a=10;
System.Console.PRintln(a):
we use the implicit conversion of int toString. If it is (String)a, it is called display. Therefore, the difference between explicit/hidden lies in whether it is displayed. Everyone is definitely still confused now. It will be clear when I write out the example tomorrow and analyze it. I have to turn off the lights. Alright, I'm taking the first step!
Oh~~~~~ I finally got up, at 8:45 on May 5th. Here is an example. In this example, a type named RomanNumeral is declared, and then He implemented several conversions.
000: // UserConversionsconversion.cs
001: using System;
002:
003: struct RomanNumeral
004: {
005: public RomanNumeral(int value)
006: {
007 : this.value = value;
008: }
009: static public implicit Operator RomanNumeral(int value)
010: {
011: return new RomanNumeral(value);
012: }
013: static public explicit operator int( RomanNumeral roman)
014: {
015: return roman.value;
016: }
017: static public implicit operator string(RomanNumeral roman)
018: {
019: return("Conversion not yet implemented");
020 : }
021: private int value;
022: }
023:
024: class Test
025: {
026: static public void Main()
027: {
028: RomanNumeral numeral;
029:
030: numeral = 10;
031:
032: // Explicit conversion from numeral to int033: Console.WriteLine((int)numeral);
034:
035: // Implicit conversion to string036: Console. WriteLine(numeral);
037:
038: // Explicitly convert to int, then explicitly convert to short040: short s = (short)numeral;
041:
042: Console.WriteLine(s);
043:
044: }
045: }
The output of this example is:
10
Conversion not yet implemented
10
Pay attention to the operator operators 009 and 013, which are conversion operators. static public explicit operator int(RomanNumeral roman), remember this form, it represents a conversion. Look at line 033 again, because the int conversion was declared as explicit, that is, explicitly, so when using this conversion, you must use parentheses.
Another example is given below. This example declares two structures, RomanNumeral and BinaryNumeral, and then converts between them.
000: // UserConversionsstructconversion.cs
001: using System;
002:
003: struct RomanNumeral
004: {
005: public RomanNumeral(int value) { this.value = value; }
006: static public implicit operator RomanNumeral(int value)
007: {return new RomanNumeral(value);}
008: static public implicit operator
009: RomanNumeral(BinaryNumeral binary)
010: {return new RomanNumeral((int)binary);}
011: static public explicit operator int(RomanNumeral roman)
012: {return roman.value;}
013: static public implicit operator string(RomanNumeral roman)
014: {return("Conversion not yet implemented");}
015: private int value;
016: }
017:
018: struct BinaryNumeral
019: {
020: public BinaryNumeral(int value) {this.value = value;}
021:
022: static public implicit operator BinaryNumeral(int value)
023: {return new BinaryNumeral(value);}
024: static public implicit operator string(BinaryNumeral binary)
025: {return("Conversion not yet implemented");}
026: static public explicit operator int(BinaryNumeral binary)
027: {return(binary.value);}
028:
029: private int value;
030: }
031:
032: class Test
033: {
034: static public void Main()
035: {
036: RomanNumeral roman;
037: roman = 10;
038: BinaryNumeral binary;
039: binary = (BinaryNumeral)(int)roman;
040: roman = binary;
041: Console.WriteLine((int)binary);
042: Console.WriteLine(binary);
043: }
044: }
The output of this example is:
10
Conversion not yet implemented
Note that line 039 is not directly converted from RomanNumeral to BinaryNumeral because there is no direct conversion provided. So first convert RomanNumeral to int, and then convert into BinaryNumeral. The rest of the things are the same as the above example (at least I think so). If you understand the above example, the following will be fine.
The above is the SUNWEN tutorial - C# Advanced (9) For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.
