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

  • Home
  • SEARCH
  • 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 724971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:17:03+00:00 2026-05-14T06:17:03+00:00

This question is about is and as in casting and about CA1800 PostSharp rule.

  • 0

This question is about “is” and “as” in casting and about CA1800 PostSharp rule. I want to know if the solution I thought is the best one possible or if it have any problem that I can’t see.

I have this code (named OriginaL Code and reduced to the minimum relevant). The function ValidateSubscriptionLicenceProducts try to validate a SubscriptionLicence (that could be of 3 types: Standard,Credit and TimeLimited ) by casting it and checking later some stuff (in //Do Whatever).

PostSharp complains about CA1800:DoNotCastUnnecessarily. The reason is that I am casting two times the same object to the same type. This code in best case will cast 2 times (if it is a StandardLicence) and in worst case 4 times (If it is a TimeLimited Licence). I know is possible to invalidate rule (it was my first approach), as there is no big impact in performance here, but I am trying a best approach.

 //Version Original Code
  //Min 2 casts, max 4 casts
  //PostSharp Complains about CA1800:DoNotCastUnnecessarily
  private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
        {
   if (licence is StandardSubscriptionLicence)
            {               
                // All products must have the same products purchased
                List<StandardSubscriptionLicenceProduct> standardProducts = ((StandardSubscriptionLicence)licence).SubscribedProducts;
                //Do whatever
            }
            else if (licence is CreditSubscriptionLicence)
            {               
                // All products must have a valid Credit entitlement & Credit interval
                List<CreditSubscriptionLicenceProduct> creditProducts = ((CreditSubscriptionLicence)licence).SubscribedProducts;
                //Do whatever
            }
            else if (licence is TimeLimitedSubscriptionLicence)
            {                
                // All products must have a valid Time entitlement
                // All products must have a valid Credit entitlement & Credit interval
                List<TimeLimitedSubscriptionLicenceProduct> creditProducts = ((TimeLimitedSubscriptionLicence)licence).SubscribedProducts;
                //Do whatever 
            }
            else
                throw new InvalidSubscriptionLicenceException("Invalid Licence type");

   //More code...


        }

This is Improved1 version using “as”. Do not complain about CA1800 but the problem is that it will cast always 3 times (if in the future we have 30 or 40 types of licences it could perform bad)

    //Version Improve 1
  //Minimum 3 casts, maximum 3 casts
  private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
        {
      StandardSubscriptionLicence standardLicence = Slicence as StandardSubscriptionLicence;
            CreditSubscriptionLicence creditLicence = Clicence as CreditSubscriptionLicence;
            TimeLimitedSubscriptionLicence timeLicence = Tlicence as TimeLimitedSubscriptionLicence;

   if (Slicence == null)
            {               
                // All products must have the same products purchased
                List<StandardSubscriptionLicenceProduct> standardProducts = Slicence.SubscribedProducts;
                //Do whatever
            }
            else if (Clicence == null)
            {               
                // All products must have a valid Credit entitlement & Credit interval
                List<CreditSubscriptionLicenceProduct> creditProducts = Clicence.SubscribedProducts;
                //Do whatever
            }
            else if (Tlicence == null)
            {                
                // All products must have a valid Time entitlement
                // All products must have a valid Credit entitlement & Credit interval
                List<TimeLimitedSubscriptionLicenceProduct> creditProducts = Tlicence.SubscribedProducts;
                //Do whatever 
            }
            else
                throw new InvalidSubscriptionLicenceException("Invalid Licence type");

   //More code...
        }

But later I thought in a best one. This is the final version I am using.

    //Version Improve 2
// Min 1 cast, Max 3 Casts
// Do not complain about CA1800:DoNotCastUnnecessarily
private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
        {
            StandardSubscriptionLicence standardLicence = null;
            CreditSubscriptionLicence creditLicence = null;
            TimeLimitedSubscriptionLicence timeLicence = null;

            if (StandardSubscriptionLicence.TryParse(licence, out standardLicence))
            {
                // All products must have the same products purchased
                List<StandardSubscriptionLicenceProduct> standardProducts = standardLicence.SubscribedProducts;
    //Do whatever
            }
            else if (CreditSubscriptionLicence.TryParse(licence, out creditLicence))
            {
                // All products must have a valid Credit entitlement & Credit interval
                List<CreditSubscriptionLicenceProduct> creditProducts = creditLicence.SubscribedProducts;
                //Do whatever
            }
            else if (TimeLimitedSubscriptionLicence.TryParse(licence, out timeLicence))
            {
                // All products must have a valid Time entitlement
                List<TimeLimitedSubscriptionLicenceProduct> timeProducts = timeLicence.SubscribedProducts;
                //Do whatever
            }
            else
                throw new InvalidSubscriptionLicenceException("Invalid Licence type");

            //More code...

        }


    //Example of TryParse in CreditSubscriptionLicence
  public static bool TryParse(SubscriptionLicence baseLicence, out CreditSubscriptionLicence creditLicence)
        {
            creditLicence = baseLicence as CreditSubscriptionLicence;
            if (creditLicence != null)
                return true;
            else
                return false;
        }

It requires a change in the classes StandardSubscriptionLicence, CreditSubscriptionLicence and TimeLimitedSubscriptionLicence to have a “tryparse” method (copied below in the code). This version I think it will cast as minimum only once and as maximum three. What do you think about improve 2? Is there a best way of doing it?

  • 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-14T06:17:04+00:00Added an answer on May 14, 2026 at 6:17 am

    Of your three code snippets, “improve 2” seems to be the best one.
    However I think you could improve your design in a way that would completely remove the need for casting.

    Add an abstract method called ValidateProducts to SubscriptionLicence and let each child licence implement the logic that is specific for that particular type of licence. This way, you place the business logic with the data, thus avoiding an anemic domain model.

    this way, the implementation of your method would merely be:

    private void ValidateSubscriptionLicenceProducts(SubscriptionLicence licence)
    {
        if(!licence.ValidateProducts())
            throw new Exception("Failed to validate products");
    }
    

    Furthermore, by making the method abstract on the base class, you enforce each “child licence” to implement the method, so you do not need to check anything. Therefore the ValidateSubscriptionLicenceProducts method will never have to be changed even though new types of licences are added in the future.

    Hope it makes sense.

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

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 373k
  • 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 No. Browsers don't have the ability to select folder, and… May 14, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer Activities that should require more than one destination (most web… May 14, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer You just missed step 5 of the tutorial. The other… May 14, 2026 at 7:30 pm

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.