显式和隐式 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1176641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:05:28  来源:igfitidea点击:

explicit and implicit c#

c#dictionaryimplicitexplicit

提问by tintincutes

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:

我是 C# 新手,正在学习新单词。我发现在编程 c# 时很难理解这两个词的含义。我查了字典的意思,这是我得到的:

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

我想在 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( )implicitlyrefer to the copies of those variables found in the object that invokes AreaPerPerson( )"

“这意味着里面的 Area 和 OccupantsAreaPerPerson( )隐含地引用了在调用的对象中找到的那些变量的副本AreaPerPerson( )

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

我很不明白这句话在这里想说什么。

采纳答案by Fredrik M?rk

The implicitand explicitkeywords in C# are used when declaring conversion operators. Let's say that you have the following class:

C# 中的implicitandexplicit关键字在声明转换运算符时使用。假设您有以下课程:

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

If you want to create a new Roleand assign a Nameto it, you will typically do it like this:

如果你想创建一个新的Role并为其分配一个Name,你通常会这样做:

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 implicitlyconvert a string to a Role(since there is no specific cast involved in the code). To achieve this, we add an implicit conversion operator:

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

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:

在这种情况下,我们不能将字符串隐式转换为 a Role,但我们需要在我们的代码中转换它:

Role r = (Role)"RoleName";

回答by Jon Skeet

In general

一般来说

  • Implicit: something is being done for you automatically.
  • Explicit: you've written something in the source code to indicate what you want to happen.
  • 隐式:自动为你做某事。
  • 明确:你在源代码中写了一些东西来表明你想要发生的事情。

For example:

例如:

int x = 10;
long y = x; // Implicit conversion from int to long
int z = (int) y; // Explicit conversion from long to int

Implicit and explicit are used quite a lot in different contexts, but the general meaning will always be along those lines.

隐式和显式在不同的上下文中使用得很多,但一般含义将始终遵循这些原则。

Note that occasionally the two can come together. For instance:

请注意,有时两者可以走到一起。例如:

int x = 10;
long y = (long) x; // Explicit use of implicit conversion!

(An explicit conversion is one which hasto be stated explicitly; an implicit version is one which canbe used implicitly, i.e. without the code having to state it.)

(显式转换是一个其中具有要被明确陈述;一个隐含的版本是一个它可以被隐式地使用,即无需声明它的代码。)

回答by Colin Mackay

Being explicit in C# is mainly about showing your intentions clearly and unambiguously.

在 C# 中明确表示主要是为了清楚明确地表达您的意图。

For example:

例如:

class MyClass
{
    string myField;

    void MyMethod(int someNumber)
    {

    }
}

In the above code the visibility of the class, field and method are all implied. They use the compiler defaults.

在上面的代码中,类、字段和方法的可见性都是隐含的。他们使用编译器默认值。

Now, I can never remember what the compiler defaults are, and maybe your colleagues can't either, so rather than rely on everyone remembering what the defaults are, you can be explicit.

现在,我永远无法记住编译器的默认值是什么,也许你的同事也不能,所以与其依赖每个人都记住默认值是什么,你可以明确一些。

public class MyClass
{
    private string myField;

    public void MyMethod(int someNumber)
    {
    }
}

回答by Gary McGill

I think this linkmakes it pretty clear what an implicit conversion is - it's one where you don't need to explicitly cast the value in an assignment. So, instead of doing

我认为这个链接很清楚隐式转换是什么 - 它是您不需要在赋值中显式转换值的一种。所以,而不是做

myDigit = (Digit) myDouble 

...you can just do:

...你可以这样做:

myDigit = myDouble;

回答by maciejkow

Consider you have two classes:

考虑你有两个类:

internal class Explicit
{
    public static explicit operator int (Explicit a)
    {
        return 5;
    }
}


internal class Implicit
{
    public static implicit operator int(Implicit a)
    {
        return 5;
    }
}

and two objects:

和两个对象:

var obj1 = new Explicit();
var obj2 = new Implicit();

you can now write:

你现在可以写:

int integer = obj2; // implicit conversion - you don't have to use (int)

or:

或者:

int integer = (int)obj1; // explicit conversion

but:

但:

int integer = obj1; // WON'T WORK - explicit cast required

Implicit conversion is meant to be used when conversion doesn't loose any precision. Explicit conversion means, that you can loose some precision and must state clearly that you know what you're doing.

隐式转换意味着在转换不会丢失任何精度时使用。显式转换意味着,您可能会失去一些精度,并且必须清楚地说明您知道自己在做什么。

There is also a second context in which implicit/explicit terms are applied - interface implementation. There are no keywords in that case.

还有第二个上下文,其中应用了隐式/显式术语 - 接口实现。在这种情况下没有关键字。

internal interface ITest
{
    void Foo();
}

class Implicit : ITest
{
    public void Foo()
    {
        throw new NotImplementedException();
    }
}

class Explicit : ITest
{
    void ITest.Foo() // note there's no public keyword!
    {
        throw new NotImplementedException();
    }
}

Implicit imp = new Implicit();
imp.Foo();
Explicit exp = new Explicit();
// exp.Foo(); // won't work - Foo is not visible
ITest interf = exp;
interf.Foo(); // will work

So when you use explicit interface implementation, interface's methods are not visible when you use concrete type. This can be used when interface is a helper interface, not part of class'es primary responsibility and you don't want additional methods to mislead someone using your code.

所以当你使用显式接口实现时,当你使用具体类型时,接口的方法是不可见的。当 interface 是一个辅助接口,而不是类的主要职责的一部分,并且您不希望其他方法误导使用您的代码的人时,可以使用此方法。

回答by Roque Mejos

Implicit can be taken as implied, but explicit means that you state it must be done yourself. Like with casts. Here is an implicit cast:

隐式可以被视为暗示,但显式意味着您声明它必须由您自己完成。就像演员表一样。这是一个隐式转换:

int implicit;
implicit = 7.5;

The value '7.5' will implicitly be cast as an int. This means the compiler does it for you.

值 '7.5' 将隐式转换为 int。这意味着编译器会为您完成。

Here is explicit:

这里是明确的:

int explicit;
explicit = (int)7.5;

Here you tell the compiler that you want it cast. You explicitly declare the conversion. Hope that helps. Source: http://cboard.cprogramming.com/cplusplus-programming/24371-implicit-explicit.html

在这里你告诉编译器你想要它强制转换。您明确声明转换。希望有帮助。资料来源:http: //cboard.cprogramming.com/cplusplus-programming/24371-implicit-explicit.html