Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Role
- {
- public string Name { get; set; }
- }
- Role role = new Role();
- role.Name = "RoleName";
- Role role = "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 };
- }
- Role r = (Role)"RoleName";
- int x = 10;
- long y = x; // Implicit conversion from int to long
- int z = (int) y; // Explicit conversion from long to int
- int x = 10;
- long y = (long) x; // Explicit use of implicit conversion!
- myDigit = (Digit) myDouble
- myDigit = myDouble;
- internal class Explicit
- {
- public static explicit operator int (Explicit a)
- {
- return 5;
- }
- }
- internal class Implicit
- {
- public static implicit operator int(Implicit a)
- {
- return 5;
- }
- }
- var obj1 = new Explicit();
- var obj2 = new Implicit();
- int integer = obj2; // implicit conversion - you don't have to use (int)
- int integer = (int)obj1; // explicit conversion
- int integer = obj1; // WON'T WORK - explicit cast required
- 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
- class MyClass
- {
- string myField;
- void MyMethod(int someNumber)
- {
- }
- }
- public class MyClass
- {
- private string myField;
- public void MyMethod(int someNumber)
- {
- }
- }
Add Comment
Please, Sign In to add comment