Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways for C # .NET to use operators?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the methods of using operators in C# .NET". In daily operations, I believe that many people have doubts about the methods of using operators in C# .NET. I have consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the methods of using operators in C# .NET?" Next, please follow the editor to study!

Overloading of operator

Operator overloading is not available in most languages, but C # does. C # allows user-defined types to overload operators by using the operate keyword to write operators as exposed static functions. Let's demonstrate the overload + operator.

We create a Complex structure type to represent a complex number, and we know that a complex number consists of real and imaginary numbers, so we can define it like this:

Public struct Complex {public double Real {get; set;} public double Imaginary {get; set;}}

Now we want to add the plural, that is:

Complex a = new Complex () {Real = 1, Imaginary = 2}; Complex b = new Complex () {Real = 4, Imaginary = 8}; Complex c = a + b

By default, custom classes cannot perform arithmetic operations. The above a + b compiles an error, and we need to overload the + operator:

Public static Complex operator + (Complex C1, Complex c2) {return new Complex {Real = c1.Real + c2.Real, Imaginary = c1.Imaginary + c2.Imaginary};}

Operators such as addition, subtraction, multiplication and division in C # can be overloaded, and some operators cannot be overloaded. For more information, please see the reference link at the end of the article.

Implicit and explicit conversion operators

We know that a subclass can be implicitly converted to a parent class, and in some cases (such as a parent class assigned by a subclass), the parent class can be explicitly converted to a subclass.

In C #, explicit and implicit conversions can also be implemented for user-defined types that do not have a child parent relationship. C # allows user-defined types to control object assignment and type conversion by using the implicit and explicit keywords. It is defined as follows:

Public static operator (myType)

Here, the result type is the method name, and the source type object is the parameter, which can only be this parameter. The second parameter cannot be defined, but the private members of its class can be accessed through the parameter object. Here is an example of both explicit and implicit conversion operators:

Public class BinaryImage {private readonly bool [] _ pixels; / / implicit conversion operator example public static implicit operator ColorImage (BinaryImage bm) {return new ColorImage (bm);} / / explicit conversion operator example public static explicit operator bool [] (BinaryImage bm) {return bm._pixels;}} public class ColorImage {public ColorImage (BinaryImage bm) {}}

In this way, we can implicitly convert BinaryImage objects to ColorImage objects and explicitly convert BinaryImage objects to bool array objects:

Var binaryImage = new BinaryImage (); ColorImage colorImage = binaryImage; / / implicit conversion bool [] pixels = (bool []) binaryImage; / / explicit conversion

And conversion operators can be defined as two-way display and implicit conversion. It can come from your type or go to your type:

Public class BinaryImage {public BinaryImage (ColorImage cm) {} public static implicit operator ColorImage (BinaryImage bm) {return new ColorImage (bm);} public static explicit operator BinaryImage (ColorImage cm) {return new BinaryImage (cm);}}

We know that the as operator is also an explicit conversion operator, so does it apply to the above situation, that is:

ColorImage cm = myBinaryImage as ColorImage

Do you think it's a problem to write like this? Please tell me the answer in the comments section.

Null condition and null joint operator

Null Conditional operator?. The Null Coalescing operator is the syntax of C # 6.0.Most people are familiar with it and it is easy to use.

?. The operator returns null immediately when the object is null, and the later code is called only when it is not null. The symbols in it? Represents the object itself, symbol. Represents a call, which can be followed by not only a property of the object, but also an indexer or method. The dragon can be connected when each segment separated by this operator is of the same type. Example:

Var bar = foo?.Value; / / equivalent to foo = = null? Null: foo.Valuevar bar = foo?.StringValue?.ToString (); / / var bar = foo?.IntValue?.ToString () is supported for the same type of each segment; / / the type of each section is different and cannot be connected, because the result type cannot be determined

If you are calling the indexer, you do not need a symbol, such as:

Var foo = new [] {1,2,3}; var bar = foo? [1]; / / equivalent to foo = = null? Null: foo [1]

The empty union operator returns the value on the right when the left is empty, otherwise it returns the value on the left. Similarly, dragon connection is supported when the type of each section is the same.

Var fizz = foo.GetBar ()? Bar;var buzz = foo? Bar?? Fizz;= > Lambda operator

The Lambda operator, namely = >, is used to define Lambda expressions and is also widely used in LINQ queries. Its general definition is as follows:

(input parameters) = > expression

Example:

String [] words = {"cherry", "apple", "blueberry"}; int minLength = words.Min ((string w) = > w.Length)

In practical application, we generally omit the type declaration of parameters:

Int minLength = words.Min (w = > w.Length)

The Lambda operator can be followed by an expression, a statement, or a statement block, such as:

/ / expression (int x, int y) = > x + y string / sentence (string x) = > Console.WriteLine (x) / / statement block (string x) = > {x + = "says Hello!"; Console.WriteLine (x);}

This operator can also be easily used to define delegate methods (in fact, the Lambda operator evolved from delegates).

Define the delegate method separately:

Void MyMethod (string s) {Console.WriteLine (s + "World");} delegate void TestDelegate (string s); TestDelegate myDelegate = MyMethod;myDelegate ("Hello")

Use the Lambda operator:

Delegate void TestDelegate (string s); TestDelegate myDelegate = s = > Console.WriteLine (s + "World"); myDelegate ("Hello")

In a class, when the implementation has only one sentence of code, you can also abbreviate the method and Setter / Getter with the Lambda operator:

Public class Test {public int MyProp {get = > 123;} public void MyMethod () = > Console.WriteLine ("Hello!");} at this point, the study of "what are the methods of using operators in C# .NET" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report