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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:14:39+00:00 2026-06-18T20:14:39+00:00

I have a IEquatable<T> method thus: public bool Equals(TravelOptions other) { if (other ==

  • 0

I have a IEquatable<T> method thus:

public bool Equals(TravelOptions other)
{
    if (other == null) return false;
    return 
        this.OutTravelType.Equals(other.OutTravelType) &  //enum
        this.BackTravelType.Equals(other.BackTravelType) & //enum
        this.OutTravelPointID.Equals(other.OutTravelPointID) & //int
        this.BackTravelPointID.Equals(other.BackTravelPointID) & //int
        this.OutTerminal.Equals(other.OutTerminal) & //string
        this.BackTerminal.Equals(other.BackTerminal) & //string
        this.OutTime.Equals(other.OutTime) & //string :(
        this.BackTime.Equals(other.BackTime) & //string
        this.Checkin.Equals(other.Checkin) & //int
        this.OutFree.Equals(other.OutFree) & //bool
        this.BackFree.Equals(other.BackFree); //bool
}

but what I need to do is add some checks for nulls for various bits in there, as currently it will throw a nullreferenceexception
is there some cunning way of doing that so it doesn’t end up being a nasty mess?
the out and back travel types are enums and are always set so checking those first. the out and back free are bools, and the travelpoints are ints, all the rest are strings.
just feel it will get awfully messy if i start having to check for nulls, unless there is some shorthand way of doing that?

thanks

  • 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-18T20:14:41+00:00Added an answer on June 18, 2026 at 8:14 pm

    For all of those properties, just use == instead of calling Equals:

    return OutTravelType == other.OutTravelType &&
           BackTravelType == other.BackTravelType &&
           ...;
    

    All of those types are either handled directly (int, bool, enum) or have overloaded == operators (string). Note the use of && instead of &, as && is short-circuiting: there’s no point in checking the other 10 properties if the first one compares non-equal.

    As an aside, it looks like you could encapsulate all the “Out” properties together and all the “Back” properties together, leaving you with just:

    return OutwardJourney == other.OutwardJourney &&
           ReturnJourney == other.ReturnJourney &&
           Checkin == other.Checkin; // Looks like this isn't part of out/back
    

    where a Journey would have a travel type, travel point ID, terminal, time and “free” (whatever that means).

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

Sidebar

Related Questions

I have a class called MyClass This class inherits IEquatable and implements equals the
I have a class Person , it implements Equals() method from IEquatable<Person> (also overrides
I have a custom generic data structure that includes a Find method: public bool
I have a class which looks like this: public class NumericalRange:IEquatable<NumericalRange> { public double
I have the following simple class public class Person : IEquatable<Person> { public bool
I have a class like this: public class Foo<T> : IEquatable<T> where T :
I have a method Public Property ConfigKeys(ByVal keyName As String) As WinItUtils.Classes.ConfigKey Get Return
I have a foolish doubt.Generally System.Object implements Equals. When I implements IEquatable interface i
I have a very simply question regarding IEquatable. Given the following basic classes: public
I have implemented the IEquatable interface in a class with the following code. public

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.