use of operator overloading of implicit & explicit operators?
Answer / karthikeyant
Implicit:
By eliminating unnecessary casts, implicit conversions can
improve source code readability. However, because implicit
conversions can occur without the programmer's specifying
them, care must be taken to prevent unpleasant surprises.
In general, implicit conversion operators should never
throw exceptions and never lose information so that they
can be used safely without the programmer's awareness. If a
conversion operator cannot meet those criteria, it should
be marked explicit.
class MyType
{
public static implicit operator int(MyType m)
{
// code to convert from MyType to int
}
}
MyType x;
// implicitly call MyType's MyType-to-int conversion
operator
int i = x;
Explicit:
declares a user-defined type conversion operator that must
be invoked with a cast. For example, this operator converts
from a class called Fahrenheit to a class called Celsius:
// Must be defined inside a class called Farenheit:
public static explicit operator Celsius(Farenheit f)
{
return new Celsius((5.0f/9.0f)*(f.degrees-32));
}
Farenheit f = new Farenheit(100.0f);
Celsius c = (Celsius)f;
Is This Answer Correct ? | 3 Yes | 0 No |
Which of the following operations can you not perform on an ado.net dataset?
What is the use of dll file in c#?
How would you describe encapsulation in c#?
If we inherit a class do the private variables also get inherited ?
Why generics are used?
What is the difference between writeline and write in c#?
Explain the difference between “system.array.clone()” and “system.array.copyto()” in c#?
what is the main differnce between const,readonly and static
3 Answers Beehive Systems, CDAC,
Why delegates are type safe in c#?
What is a dynamic assembly?
How many types of interface are there in c#?
Does c# do array bounds checking?