显式和隐式c# [英] explicit and implicit c#

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

问题描述

我刚刚接触C#并学习新词。在编程c#时,我发现很难理解这两个词的含义。
我在字典中查找含义,这里是我所得到的:



隐式


隐含的东西以间接的方式表达。



如果质量或元素隐含在一些东西,它涉及它或由它显示;


显式


明确公开地表达或显示的东西,没有任何企图隐藏任何东西



如果你是明确的,你会非常公开和清楚地说出来。


我想了解一下C#。



感谢您的帮助。



干杯






附加信息:



这是书中的一部分句子,我现在正在阅读这个单词implicit


这意味着 AreaPerPerson() 隐藏中的区域和占有者是指在对象中找到的那些变量的副本, code> AreaPerPerson()


我很不明白这个句子在这里试试

解决方案

implicit explicit 关键字。假设你有以下类:

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

如果您要创建一个新的 / code>并分配一个名称,你通常会这样做:

 角色角色=新角色(); 
role.Name =RoleName;

由于它只有一个属性,如果我们可以这样做,这可能会很方便:

 角色角色=RoleName; 

这意味着我们希望隐藏将字符串转换为角色(因为代码中没有特定的转换)。为了实现这一点,我们添加一个隐式转换运算符:

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

另一个选项是实现一个显式的转换运算符:

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

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

 角色r = ) ROLENAME; 


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:

Implicit

"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;"

Explicit

"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."

I would like to understand it in C#.

Thanks for your help.

Cheers


additional info:

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

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

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

解决方案

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天全站免登陆