这些括号是什么以及如何使用它们? [英] What are these parentheses and how do I use them?

查看:116
本文介绍了这些括号是什么以及如何使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我目前正在学习C#,在教科书中我通过课程时遇到了这段代码。

 class Employee:IComparable 
{
public int IdNumber {get; set;}
public double Salary {get; set;}
int IComparable.CompareTo(Object o)
{
int returnVal;
员工临时=(员工)o;
if(this.IdNumber> temp.IdNumber)
returnVal = 1;
else
if(this.IdNumber< temp.IdNumber)
returnVal = -1;
else
returnVal = 0;
返回returnVal;
}
}

在第8行,它说:

员工临时=(员工)o; 

似乎绑定对象"o"到班级"员工"。在整本教科书中,我没有看到像这样使用括号,我想要一个解释,我觉得这是最好的地方......


提前谢谢

  - Michael

解决方案

 括号用于投射(在本例中)o来键入Employee。请参阅

以下文档

So I am currently learning C# and in the textbook I came across this block of code while going through Classes.

class Employee : IComparable
{
 public int IdNumber {get; set;}
 public double Salary {get; set;}
 int IComparable.CompareTo(Object o)
 {
 int returnVal;
 Employee temp = (Employee)o;
 if(this.IdNumber > temp.IdNumber)
 returnVal = 1;
 else
 if(this.IdNumber < temp.IdNumber)
 returnVal = -1;
 else
 returnVal = 0;
 return returnVal;
 }
}

In line 8 where it says:

Employee temp = (Employee)o;

it seems to bind the object "o" to the class "Employee". In the entire textbook I have not seen the use of parenthesis like this and I would like an explanation and I feel like this is the best place to ask...

Thanks in advance
 - Michael

解决方案

The parentheses are used to cast (in this case) o to type Employee. See the following documentation.


这篇关于这些括号是什么以及如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆