C#运算符重载实例解析

开发 后端
C#运算符重载实例向你讲解了C#运算符重载的应用,具体的操作的内容,希望对你有所帮助。

C#运算符重载实例是学习C#运算符重载的重要途径。让我们来看看下面的C#运算符重载实例吧:

  1. using System;  
  2. class hello  
  3. ...{//逻辑运算符 & | ! 按位运算  
  4.     static void Main()  
  5.     ...{//rectangle长方形[rek tan gl]  
  6.         string sName;int sInt;  
  7.     Console.Write("Input Name:");       sName=Console.ReadLine();  
  8.     Console.Write("Input a Number:");   sInt = int.Parse(Console.ReadLine());  
  9.  
  10.       
  11.     clsString myStr=new clsString();      
  12.         myStr.sL = sName;   myStr++;//利用C#运算符重载之重载的++运算符使其转变为大写  
  13.     clsNot myNotStr = new clsNot();  
  14.     myNotStr.sL = sName; myNotStr=!myNotStr;//利用C#运算符重载之重载的++运算符使其转变为大写  
  15.       
  16.     Console.WriteLine("The Enter Number is: {0}", sInt);  
  17.     clsInt myInt = new clsInt();          
  18.         myInt.sL = sInt;    myInt++;  
  19.     object ob = myInt.sL;//装箱  
  20.  
  21.     AdverseCase mStr = new AdverseCase(); mStr.sL = sName;//利用类的全局变量转换  
  22.     string c = myStr;  
  23.     Console.WriteLine("Upper Name is: {0}", c);//隐含转换成字符串  
  24.     Console.WriteLine("Your  Name is: {0}", sName);  
  25.     Console.WriteLine("Not   Name is: {0}", myNotStr.sL);  
  26.     Console.WriteLine("Cls   Name is: {0}", mStr.ToAdverseCase());  
  27.     Console.WriteLine("The Number Increase is: {0}", ob);  
  28.     Console.ReadLine();  
  29.     }  
  30. }  
  31. class clsString//C#运算符重载之重载++运算符使字符串变大写
  32. //如果要重载++运算符使其能直接用于string怎么处理  
  33. ...{  
  34.     public string sL;  
  35.     public static clsString operator ++(clsString strIn)  
  36.     ...{  
  37.         clsString cs = new clsString();  
  38.         cs.sL = strIn.sL.ToUpper();  
  39.         return cs;  
  40.     }  
  41.     //隐含转换public static implicit operator string(clsString c)  
  42.     //明确转换public static explicit operator string(clsString c)  
  43.     //重载 类型转换 由clsString转换为 string                       
  44.     public static implicit operator string(clsString c)  
  45.     ...{  
  46.         string i; i = c.sL;  
  47.         return i;  
  48.     }  
  49. }  
  50. class clsInt//C#运算符重载之重载++运算符使整形数据每次增加10  
  51. ...{  
  52.     public int sL;  
  53.     public static clsInt operator ++(clsInt strIn)  
  54.     ...{  
  55.         clsInt ci = new clsInt();  
  56.         ci.sL = strIn.sL+10;  
  57.         return ci;  
  58.     }  
  59. }  
  60. class clsNot//利用C#运算符重载之运算符 ! 重载转换翻转字符串大小写  
  61. ...{  
  62.     public string sL;  
  63.     public static clsNot operator !(clsNot strIn)  
  64.     ...{  
  65.         clsNot ci = new clsNot();  
  66.         ci.sL = RevServer(strIn.sL);  
  67.         return ci;  
  68.     }  
  69.     protected static string RevServer(string s)  
  70.     ...{//大写变小写,小写变大写  
  71.         char[] ct;  
  72.         if (s.Length == 1)  
  73.         ...{  
  74.             ct = s.ToCharArray();   
  75.             if (ct[0] >= 'a')s = s.ToUpper();else s = s.ToLower();  
  76.             return s;  
  77.         }  
  78.         string sTmp = s.Substring(0, 1);  
  79.         ct = sTmp.ToCharArray();   
  80.         if (ct[0] >= 'a')  
  81.             sTmp = sTmp.ToUpper();  
  82.         else 
  83.             sTmp = sTmp.ToLower();  
  84.         sTmp +=  RevServer(s.Substring(1));  
  85.         return sTmp;  
  86.     }  
  87. }  
  88. //利用类成员转换翻转字符串大小写  
  89. class AdverseCase  
  90. ...{  
  91.     public string sL;  
  92.     public string ToAdverseCase()  
  93.     ...{  
  94.         return RevServer(this.sL);  
  95.     }  
  96.     protected static string RevServer(string s)  
  97.     ...{//大写变小写,小写变大写  
  98.         char[] ct;  
  99.         if (s.Length == 1)  
  100.         ...{  
  101.             ct = s.ToCharArray();  
  102.             if (ct[0] >= 'a') s = s.ToUpper(); else s = s.ToLower();  
  103.             return s;  
  104.         }  
  105.         string sTmp = s.Substring(0, 1);  
  106.         ct = sTmp.ToCharArray();  
  107.         if (ct[0] >= 'a')  
  108.             sTmp = sTmp.ToUpper();  
  109.         else 
  110.             sTmp = sTmp.ToLower();  
  111.         sTmp += RevServer(s.Substring(1));  
  112.         return sTmp;  
  113.     }  
  114. }  

C#运算符重载实例的内容就向你介绍到这里,希望对你学习C#运算符重载有所帮助。

【编辑推荐】

  1. C#运算符种类简析
  2. C#位运算符种类及使用浅析
  3. C#运算符重载实例浅析
  4. C#运算符重载概念及应用详解
  5. C#运算符重载实现复数运算
责任编辑:仲衡 来源: CSDN博客
相关推荐

2009-08-12 10:27:12

C#运算符重载运算符重载实例

2009-08-12 10:47:03

C#运算符重载

2009-09-04 13:18:10

C#允许运算符重载

2009-08-14 10:16:57

C#运算符重载

2009-08-12 12:46:11

C#运算符重载

2009-08-12 10:37:13

C#运算符重载

2009-08-12 11:20:51

C#运算符重载

2009-08-11 15:51:08

C#运算符算术运算符

2009-08-12 15:20:18

C#赋值运算符复合赋值运算符

2009-08-12 09:30:10

C#??运算符

2009-08-12 15:02:49

C#赋值运算符简单赋值运算符

2009-08-12 14:49:33

C#移位运算符

2021-12-15 10:25:57

C++运算符重载

2011-07-15 01:34:36

C++重载运算符

2009-09-01 10:08:57

C#运算符

2009-08-12 13:35:22

C#关系运算符

2009-08-11 14:16:38

C# New运算符

2009-08-12 14:29:32

C#条件运算符

2009-11-06 13:57:52

C#

2009-08-12 14:23:09

C#逻辑运算符
点赞
收藏

51CTO技术栈公众号