简体   繁体   中英

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#. I don't want to define it in 2 lines. I want to define a full program to show implicit and explicit type conversion in 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:

// 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 ).

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

Implicit and explicit type conversion in C# is similiar to C++ and aother OOP languages.

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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