简体   繁体   English

请给我一个C#中隐式和显式类型转换的例子

[英]Please give me example of implicit and explicit type conversion in C#

can any one give me implicit type conversion example in real life. 任何人都可以在现实生活中给我隐式类型转换的例子。 I know implicit type conversion means conversion from derived to base class but i don't know how to show in coding in c#. 我知道隐式类型转换意味着从派生到基类的转换,但我不知道如何在c#中进行编码。 I don't want to define it in 2 lines. 我不想在2行中定义它。 I want to define a full program to show implicit and explicit type conversion in c#. 我想定义一个完整的程序来显示c#中的隐式和显式类型转换。 Please help me. 请帮我。

Regards 问候

No, implicit type conversion just means a type conversion which doesn't need to be explicit in the code. 不,隐式类型转换只是意味着一种类型转换,它不需要在代码中显式化。

LINQ to XML provides good examples: LINQ to XML提供了很好的例子:

// Implicit conversion from string to XNamespace
XNamespace ns = "http://url.com";

XElement element = new XElement("foo", "bar");
// Explicit conversion of XElement to string
string value = (string) element;

So that's how they're used - and you create your own explicit or implicit conversion operators using the kind of code shown in MSDN ( explicit , implicit ). 这就是它们的使用方式 - 您可以使用MSDN中显示的代码( 显式隐式创建自己的显式或隐式转换运算符。

Short, complete, but pointless example: 简短,完整,但毫无意义的例子:

class Foo
{
    private readonly int value;

    private Foo(int value)
    {
        this.value = value;
    }

    public static implicit operator Foo(int value)
    {
        return new Foo(value);
    }

    public static explicit operator int(Foo foo)
    {
        if (foo == null)
        {
            throw new ArgumentException("foo");
        }
        return foo.value;
    }
}

class Test
{    
    static void Main(string[] args)
    {
        int x = 10;
        Foo foo = x;
        int y = (int) foo;
    }
}

Implicit (This is just an example, there are other situations in which an object's type is implicitly converted.) 隐式 (这只是一个示例,还有其他情况,其中隐式转换对象的类型。)

int f(Animal a) {...}

Elephant e; // Elephant is-a Animal
f(e);

Explicit 明确的

int f(Animal a) {...}

Alien someAlien; // Alien is-not-a Animal
f((Animal)someAlien); // Works only if conversion from Alien to Animal is user-defined.

Probably the most interesting part of my answer will be to tell you to refer to Casting and Type Conversions (C# Programming Guide) for a full explanation of the different type of conversions in C#, and secondly to Conversion Operators (C# Programming Guide) . 可能我的答案中最有趣的部分是告诉你参考Casting和Type Conversions(C#编程指南) ,以获得C#中不同类型转换的完整解释,其次是转换操作符(C#编程指南)

Implicit and explicit type conversion in C# is similiar to C++ and aother OOP languages. C#中的隐式和显式类型转换类似于C ++和其他OOP语言。

If the conversion is not going to cause any dataloss, conversion will occur automatically. 如果转换不会导致任何数据转换,则会自动进行转换。 Nothing else needs to be done.: 没有其他事情需要做:

int i = 10;
double j = 20.1;

j = i;

// j = 10.

if the conversion will cause dataloss, then you must explicitly state the type that the subject will be converted to: 如果转换将导致dataloss,那么您必须明确说明主题将转换为的类型:

int i = 10;
double j = 20.1;

i = (int) j;

// i = 10

This is the most basic example, other conversions will occur when you work cast objects according to their intheritance tree... 这是最基本的示例,当您根据其intheritance树工作转换对象时,将发生其他转换...

Assume that we have a 1 lit empty bottle and a 1/2 lit bottle.Suppose I want to transfer the water from second bottle to the first bottle.As as the 1st container is bigger ,it is capable to contain the whole water. 假设我们有一个1升的空瓶和1/2个瓶子。假设我想将水从第二个瓶子转移到第一个瓶子。当第一个容器更大时,它能够容纳整个水。 eg 例如

int i=10; //i is 4 bytes. 
long l=i;//l is 8 bytes.

This type of conversion is called Implicit conversion. 这种类型的转换称为隐式转换。 Suppose we are transferring the whole water from big container to small container.Then there is a chance of loosing data and also the code will not be executed. 假设我们将整个水从大容器转移到小容器。然后有可能丢失数据并且代码也不会被执行。 eg 例如

long l=199;
int i=(int)l;

//till when the capacity of int value is satisfying it is possible to copy. //当int值的容量满足时,可以复制。 This is called Explicit Conversion 这称为显式转换

Built in types: 内置类型:

byte smallNumber = 255;

// byte implicitly casted to int
int num = smallNumber;

// explicitly cast byte to int
num = (int)smallNumber;

Custom types: 自定义类型:

public abstract class AnimalBase
public sealed class Tiger : AnimalBase

Tiger tiger = new Tiger();

// explicit (but really does not required to be specified)
AnimalBase anotherAnimal = (AnimalBase)tiger;

// implicit Tiger to AnimalBase
AnimalBase anotherAnimal = tiger;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM