显性和隐性的C# [英] explicit and implicit c#

查看:143
本文介绍了显性和隐性的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C#和学习新单词。我觉得很难理解什么是当它涉及到编程C#这两个词的含义。
我看着在字典中的意思和这里就是我得到的答案:

I'm new to C# and learning new words. I find it difficult to understand what's the meaning of these two words when it comes to programming c#. I looked in the dictionary for the meaning and here's what I got:

的东西是隐含以间接的方式来表示。

"Something that is implicit is expressed in an indirect way."

如果一质量或元件处于隐的东西,它是参与,或由它显示;

"If a quality or element is implicit in something, it is involved in it or is shown by it;"

明确

的东西是明确的明示或明确公开表示,没有任何企图隐瞒什么

"Something that is explicit is expressed or shown clearly and openly, without any attempt to hide anything"

如果你是显性的东西,你很公开和明确地谈论它。

"If you are explicit about something, you speak about it very openly and clearly."

我想了解它在C#。

感谢您的帮助。

干杯


更多信息:

下面是句子的什么书,我现在在读的一部分有这个词的隐式

Here is a part of sentence in the book what I'm reading now which has this word "implicit"

这意味着,区域和内部 AreaPerPerson住户() 是指那些副本在调用 AreaPerPerson()

"This means that Area and Occupants inside AreaPerPerson( ) implicitly refer to the copies of those variables found in the object that invokes AreaPerPerson( )"

我挺唐对象发现变量'T明白这句话在这里想说的。

I quite don't understand what this sentence here trying to say.

推荐答案

和的 明确在C#中关键字声明转换运营商时使用。假设你有下面的类:

The implicit and explicit keywords in C# are used when declaring conversion operators. Let's say that you have the following class:

public class Role
{
    public string Name { get; set; }
}

如果你想创建一个新的角色并分配名称来,你通常会做这样的:

If you want to create a new Role and assign a Name to it, you will typically do it like this:

Role role = new Role();
role.Name = "RoleName";



由于它只有一个属性,它或许会很方便,如果我们能够代替像这样做:

Since it has only one property, it would perhaps be convenient if we could instead do it like this:

Role role = "RoleName";

这意味着,我们要的的字符串转换为角色(因为没有涉及特定代码转换)。要做到这一点,我们添加的隐式转换操作符:

This means that we want to implicitly convert a string to a Role (since there is no specific cast involved in the code). To achieve this, we add an implicit conversion operator:

public static implicit operator Role(string roleName)
{
    return new Role() { Name = roleName };
}



另一个选择是实现一个明确的转换操作符:

Another option is to implement an explicit conversion operator:

public static explicit operator Role(string roleName)
{
    return new Role() { Name = roleName };
}

在这种情况下,我们不能隐字符串转换为角色,但我们需要转换它在我们的代码:

In this case, we cannot implicitly convert a string to a Role, but we need to cast it in our code:

Role r = (Role)"RoleName";

这篇关于显性和隐性的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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