技术控

    今日:0| 主题:63445
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] C# – Operator Overloading

[复制链接]
一厘米的距离ゃ 发表于 2016-10-2 03:31:47
386 6
In this article, we will discuss about operator overloading in C#.
  Background:

  Operator overloading is very Like Method Overloading. All operator Overloading methods are static methods of the class. In this article we will calculate area of two squares using Operator Overloading. Operator will be used between objects of class. Here, I am going to use console Application of visual studio.
  The Complete list of Operators that can be overloaded are:

  
       
  • Unary Operator : +,-,!,~,++,–,true,false   
  • Binary Operator :+,-,*,/,%,&,|,^,<<,>>,==,!=,>,<,>=,<=  
  The operator which cannot be overloaded

  =,&&,||,?:,Cheeked, new, typeof, as, and is operator.
  Now we will create Square class. Below is the following codes.

  1. public class Square
  2.     {
  3.         public decimal Length { set; get; }
  4.         public decimal Width { set; get; }
  5.         public static Squareoperator *(Square_squareObj1, Square_squareObj2)
  6.         {
  7.             Square_squareObj = new Square();
  8.             _squareObj.Length = _squareObj1.Length * _squareObj2.Length;
  9.             _squareObj.Width = _squareObj1.Width * _squareObj2.Width;
  10.             return _squareObj;
  11.         }
  12.         public decimal Area()
  13.         {
  14.             return Length * Width;
  15.         }
  16.     }
复制代码
Let’s get explanation about the code:

  1. public class Square
复制代码
Here, square is the class name.

  1. public decimal Length { set; get; }
  2. public decimal Width { set; get; }
复制代码
There are two property of Square Class. One is Length which has decimal data type. And other is width which has decimal type.
  1.         public static Squareoperator *(Square_squareObj1, Square_squareObj2)
  2.         {
  3.             Square_squareObj = new Square();
  4.             _squareObj.Length = _squareObj1.Length * _squareObj2.Length;
  5.             _squareObj.Width = _squareObj1.Width * _squareObj2.Width;
  6.             return _squareObj;
  7.         }
复制代码
The above method implements the Multiplication operator (*) for a user-defined class Square. This method is a static method which have tow parameter. Both are Square’s class objects and both objects done multiplication.
  1. public decimal Area()
  2. {
  3.     return Length * Width;
  4. }
复制代码
We have Multiplication Length and Width.
  Now we will call Square class in Main function:

  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             // First Square
  6.             Square_obj1 = new Square();
  7.             _obj1.Length = 40;
  8.             _obj1.Width = 40;
  9.             // Second Square
  10.             Square_obj2 = new Square();
  11.             _obj2.Length = 50;
  12.             _obj2.Width = 50;
  13.             // For match tow Object
  14.             Square_obj3 = new Square();
  15.             _obj3 = _obj1 * _obj2;
  16.             // Assign area to variable
  17.             decimal area = _obj3.Area();
  18.             Console.WriteLine(area);
  19.             Console.ReadKey();
  20.         }
  21.     }
复制代码
Let’s get explanation about the code:

  We have calculated total area of two squares.
  1. Square_obj1 = new Square();
  2. _obj1.Length = 40;
  3. _obj1.Width = 40;
复制代码
We have create object which name given _obje1. We have assigned length of square 40 and  width of square 40
  1. Square_obj2 = new Square();
  2. _obj2.Length = 50;
  3. _obj2.Width = 50;
复制代码
Again, we have create object which name given _obje2. We have assigned length of square 50 and other width of square 50.
  1. Square_obj3 = new Square();
  2. _obj3 = _obj1 * _obj2;
复制代码
New Object have been created for multiplication two object.
  1. decimal area = _obj3.Area();
  2. Console.WriteLine(area);
  3. Console.ReadKey();
复制代码
The Area method have been called and assigned result to area variable.
   

C# – Operator Overloading

C# – Operator Overloading

  Hope this will be helpful ��
方力眠n 发表于 2016-10-2 05:58:51
方力眠n涨姿势了
回复 支持 反对

使用道具 举报

haining770 发表于 2016-10-2 06:22:43
锻炼肌肉,防止挨揍!  
回复 支持 反对

使用道具 举报

redak 发表于 2016-10-2 06:53:02
别回头,哥恋的只是你的背影。
回复 支持 反对

使用道具 举报

雅柔 发表于 2016-10-2 11:45:58
友情提示:由于此用户的签名太过于个性而被系统自动屏蔽
回复 支持 反对

使用道具 举报

66644383 发表于 2016-11-7 10:49:53
撸过。。。。
回复 支持 反对

使用道具 举报

new5201314 发表于 2016-11-11 13:32:56
不作死就不会死
回复 支持 反对

使用道具 举报

我要投稿

回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表