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 8065745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:45:49+00:00 2026-06-05T11:45:49+00:00

For educational purposes I do lots of things (that maybe never used) with casting.

  • 0

For educational purposes I do lots of things (that maybe never used) with casting. One of them lead me to solution that I dislike.

Let’s say, I have 2 classes:

class ClassA
{
    public int MyInt;
    public string MyString;
}

class ClassB
{
    public int SomeInt;
}

Now I want implicitly cast ClassA to ClassB. To do that I can write:

public static implicit operator ClassB(ClassA obj1)
{
    ClassB obj2 = new ClassB();
    obj2.SomeInt = obj1.MyInt;

    return obj2;
}

But there’s a possibility that MyString get lost, that must not be allowed. To solve this problem I’ve added to my conversion:

if (obj1.MyString != null)
   throw new DataLostException();

This works fine, but there must be simpler solution. My question is, How can I handle this kind of data loss in a simpler way?

  • 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-06-05T11:45:51+00:00Added an answer on June 5, 2026 at 11:45 am

    Your solution is correct. Such data loss is the “default behavior” when creating a user defined conversion operator, which is a relatively advanced use of the language.

    There is no inheritance relation between the two classes, and there is no way for the framework to know how that string might relate to the other class. Who knows, it might be a piece of diagnostic information that is completely unnecessary in the new context.

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

Sidebar

Related Questions

I need both of them on one computer, first for educational purposes and second
For pure educational purposes, what should I write instead of * such that I
For educational purposes, I'm writing a set of methods that cause runtime exceptions in
Any ideas where can I find a free (trial or for educational purposes) WebSphere
My git repository has ~2,000 commits. For educational purposes, I have been playing around
I'm starting a new project that involves users paying to see educational videos. These
For educational purposes I have set up a project layout like so (flat in
Hey Simple little app for educational purposes, its a simple uitableview and I want
I am creating an RSS reader purely for my own educational purposes and am
I'd like to develop a PC LCD display emulator for educational purposes. The display

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.