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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:43:16+00:00 2026-05-10T13:43:16+00:00

I have a Status class in C#, used like this: Status MyFunction() { if(…)

  • 0

I have a ‘Status’ class in C#, used like this:

Status MyFunction() {    if(...) // something bad      return new Status(false, 'Something went wrong')    else      return new Status(true, 'OK'); } 

You get the idea. All callers of MyFunction should check the returned Status:

Status myStatus = MyFunction(); if ( ! myStatus.IsOK() )    // handle it, show a message,... 

Lazy callers however can ignore the Status.

MyFunction(); // call function and ignore returned Status 

or

{   Status myStatus = MyFunction();  } // lose all references to myStatus, without calling IsOK() on it 

Is it possible to make this impossible? e.g. an throw exception

In general: is it possible to write a C# class on which you have to call a certain function?

In the C++ version of the Status class, I can write a test on some private bool bIsChecked in the destructor and ring some bells when someone doesn’t check this instance.

What is the equivalent option in C#? I read somewhere that ‘You don’t want a destructor in your C# class’

Is the Dispose method of the IDisposable interface an option?

In this case there are no unmanaged resources to free. Additionally, it is not determined when the GC will dispose the object. When it eventually gets disposed, is it still possible to know where and when you ignored that specific Status instance? The ‘using’ keyword does help, but again, it is not required for lazy callers.

  • 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. 2026-05-10T13:43:16+00:00Added an answer on May 10, 2026 at 1:43 pm

    I am fairly certain you can’t get the effect you want as a return value from a method. C# just can’t do some of the things C++ can. However, a somewhat ugly way to get a similar effect is the following:

    using System;  public class Example {     public class Toy     {         private bool inCupboard = false;         public void Play() { Console.WriteLine('Playing.'); }         public void PutAway() { inCupboard = true; }         public bool IsInCupboard { get { return inCupboard; } }     }      public delegate void ToyUseCallback(Toy toy);      public class Parent     {         public static void RequestToy(ToyUseCallback callback)         {             Toy toy = new Toy();             callback(toy);             if (!toy.IsInCupboard)             {                 throw new Exception('You didn't put your toy in the cupboard!');             }         }     }      public class Child     {         public static void Play()         {             Parent.RequestToy(delegate(Toy toy)             {                 toy.Play();                 // Oops! Forgot to put the toy away!             });         }     }      public static void Main()     {         Child.Play();         Console.ReadLine();     } } 

    In the very simple example, you get an instance of Toy by calling Parent.RequestToy, and passing it a delegate. Instead of returning the toy, the method immediately calls the delegate with the toy, which must call PutAway before it returns, or the RequestToy method will throw an exception. I make no claims as to the wisdom of using this technique — indeed in all ‘something went wrong’ examples an exception is almost certainly a better bet — but I think it comes about as close as you can get to your original request.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since i haven’t found any free library I decided to… May 11, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer ListView is an ItemsControl. ItemsControl.Items does not return the child… May 11, 2026 at 11:50 pm
  • Editorial Team
    Editorial Team added an answer I guess you need to give the caller the proper… May 11, 2026 at 11:50 pm

Related Questions

I have a Status class in C#, used like this: Status MyFunction() { if(...)
Has any one used BITs in VB.NET? If so, do you have code samples
I'm creating a package that is going to be used by R (the statistical
I'm working on an editor for files that are used by an important internal
I am having some issues with using the OrderBy extension method on a LINQ

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.