Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7052483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:23:21+00:00 2026-05-28T03:23:21+00:00

I have this piece of code: public class Leg : ProxiestChild { public virtual

  • 0

I have this piece of code:

public class Leg : ProxiestChild
{
    public virtual Name { get; set; }
}

the problem is:

var leg = new Leg(); // leg is not Leg, instead ProxiedLeg

var trueleg = (Leg)leg; // exception leg is a ProxiedLeg

i need something like this

public class ProxiestChild
{
    // some method that overloads explicit CAST
    // where receiving the proxied object i returns the unproxied object
    // to be casted
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:23:21+00:00Added an answer on May 28, 2026 at 3:23 am

    You can implement custom type casting using the conversion operators implicit or explicit.

    Conversion operators can be explicit or implicit. Implicit conversion operators are easier to use, but explicit operators are useful when you want users of the operator to be aware that a conversion is taking place. This topic demonstrates both types.

    Example

    This is an example of an explicit conversion operator. This operator converts from the type Byte to a value type called Digit. Because not all bytes can be converted to a digit, the conversion is explicit, meaning that a cast must be used, as shown in the Main method.

    struct Digit
    {
        byte value;
    
        public Digit(byte value)  //constructor
        {
            if (value > 9)
            {
                throw new System.ArgumentException();
            }
            this.value = value;
        }
    
        public static explicit operator Digit(byte b)  // explicit byte to digit conversion operator
        {
            Digit d = new Digit(b);  // explicit conversion
    
            System.Console.WriteLine("Conversion occurred.");
            return d;
        }
    }
    
    class TestExplicitConversion
    {
        static void Main()
        {
            try
            {
                byte b = 3;
                Digit d = (Digit)b;  // explicit conversion
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine("{0} Exception caught.", e);
            }
        }
    }
    // Output: Conversion occurred.
    

    This example demonstrates an implicit conversion operator by defining a conversion operator that undoes what the previous example did: it converts from a value class called Digit to the integral Byte type. Because any digit can be converted to a Byte, there’s no need to force users to be explicit about the conversion.

    struct Digit
    {
        byte value;
    
        public Digit(byte value)  //constructor
        {
            if (value > 9)
            {
                throw new System.ArgumentException();
            }
            this.value = value;
        }
    
        public static implicit operator byte(Digit d)  // implicit digit to byte conversion operator
        {
            System.Console.WriteLine("conversion occurred");
            return d.value;  // implicit conversion
        }
    }
    
    class TestImplicitConversion
    {
        static void Main()
        {
            Digit d = new Digit(3);
            byte b = d;  // implicit conversion -- no cast needed
        }
    }
    // Output: Conversion occurred.
    

    from: http://msdn.microsoft.com/en-us/library/85w54y0a(v=VS.100).aspx

    Do be careful with this, for readability it can often be confusing to see one type magically cast to another – people don’t always first think that there are conversion operators in play.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this piece of code public class Test{ public static void main(String[]
I have got this piece of code: public class ThreadInteraction { public static void
I have this piece of code: public class Authenticate : ActionFilterAttribute { public override
I have this piece of code: class Test { public static void main (String[]
I have this piece of code : public class Profile extends Container{ public Profile(String
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code: <li class=selected><a href=/AmIACandidate.html>Am I a Candidate?</a></li> I need
I have this piece of code in javascript: var catId = $.getURLParam(category); In my
Consider this piece of code: public abstract class Validator { protected Validator() { }
I have this piece of code from GWT in Action: public void processOperator(final AbstractOperator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.