当前位置:网站首页>C Entry series (31) -- operator overloading
C Entry series (31) -- operator overloading
2022-07-26 08:42:00 【InfoQ】
// Modifier return type operator Overloadable operators ( parameter list )
public static Student operator +(Student a, Student b)
{
// Method body ;
}
Overloadable and non overloadable operators
Code example
using System;
namespace _6_2_1 Operator overloading
{
class Program
{
static void Main(string[] args)
{
Student st1 = new Student();
Student st2 = new Student();
Student st3 = new Student();
st1._acore = 40;
st2._acore = 50;
Console.WriteLine("st1 and st2 Whether the results are the same :" + (st1 == st2));
Console.WriteLine("st1 and st2 Whether the results are the same :" + (st1 != st2));
Console.WriteLine(" Performance before calculation :{0},{1},{2}", st1._acore, st2._acore, st3._acore);
// Add objects
st3 = st1 + st2;
// The compiler automatically reloads +=
st1 += st2;
Console.WriteLine(" Result after calculation :{0},{1},{2}",st1._acore,st2._acore,st3._acore);
Console.ReadKey();
}
}
class Student
{
private int acore;
public int _acore {
get { return acore; }
set { acore = value; }
}
//+ heavy load : Object addition , Get student grades and
public static Student operator +(Student b, Student c)
{
Student stu = new Student();
stu.acore = b.acore + c.acore;
return stu;
}
// When overloading == when , Must overload !=, Otherwise, compile and report an error
public static bool operator ==(Student b , Student c)
{
bool result = false;
if(b.acore == c.acore)
{
result = true;
}
return result;
}
public static bool operator !=(Student b, Student c)
{
bool result = false;
if (b.acore != c.acore)
{
result = true;
}
return result;
}
}
}

边栏推荐
- [untitled]
- [recommended collection] MySQL 30000 word essence summary + 100 interview questions (I)
- General file upload vulnerability getshell of a digital campus system (penetration test -0day)
- 基于C#实现的文件管理文件系统
- Error handling response: Error: Syntax error, unrecognized expression: .c-container /deep/ .c-contai
- PXE principles and concepts
- Code cloud change remote warehouse command
- Leetcode and query question summary
- Grid segmentation
- Logic of data warehouse zipper table
猜你喜欢
How to safely delete a useless activity in Android studio
Cadence (x) wiring skills and precautions
Excel find duplicate lines
Poor English, Oracle OCP or MySQL OCP exam can also get a high score of 80 points
Arbitrum launched the anytrust chain to meet the diverse needs of ecological projects
IC's first global hacking bonus is up to US $6million, helping developers venture into web 3!
Memory management based on C language - Simulation of dynamic partition allocation
Winter vacation homework & Stamp cutting
[freeswitch development practice] user defined module creation and use
六、品达通用权限系统__pd-tools-log
随机推荐
Oracle 19C OCP 1z0-082 certification examination question bank (42-50)
A summary of practical websites that won't brighten people's eyes
23.8 using the applicationrunner or commandlinerunner to implement applicationrunner and commandlinerunner
shell编程
Why reserve a capacitor station on the clock output?
MySQL 8.0 OCP (1z0-908) has a Chinese exam
Kotlin function
When developing flutter, idea_ ID cannot solve the problem
Oracle 19C OCP 1z0-083 question bank (7-12)
In the first year of L2, the upgrade of arbitrum nitro brought a more compatible and efficient development experience
[untitled]
[suggestions collection] summary of MySQL 30000 word essence - locking mechanism and performance tuning (IV) [suggestions collection]
23.10 Admin features
SSH,NFS,FTP
Kept dual machine hot standby
【搜索专题】看完必会的搜索问题之洪水覆盖
import error: ‘Icon‘ is not exported from ‘antd‘. Import icon error
Number of briquettes & Birthday Candles & building blocks
03异常处理,状态保持,请求钩子---04大型项目结构与蓝图
sklearn 机器学习基础(线性回归、欠拟合、过拟合、岭回归、模型加载保存)