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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:20:28+00:00 2026-06-10T12:20:28+00:00

Consider this struct. public struct myStruct{ public int value1; public int value2; public int

  • 0

Consider this struct.

public struct myStruct{
    public int value1;
    public int value2;
    public int value3;

    public myStruct(int val1, int val2, int val3){
        value1 = val1;
        value2 = val2;
        value3 = val3;
    }
}

Now consider that I have some list of type myStruct:

public List<myStruct> myList = new List<myStruct>();

Now I instantiate 2 different versions of myStruct using the same values:

myStruct s1 = new myStruct(1,2,3);
myStruct s2 = new myStruct(1,2,3);

I then add the first instance to myList:

myList.Add(s1);

Then I do a simple check to see if the second instance is in the list:

bool structsEqual = myList.Contains(s2);

Is structsEqual true or false?

  • 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-10T12:20:30+00:00Added an answer on June 10, 2026 at 12:20 pm

    Interestingly this specific code will equal true but if the struct contained objects there were not system types, then you would have to implement IComparable for this to not always return false.

    public struct myStruct
    { 
        public MyOwnType value1; 
        public int value2; 
        public int value3; 
    
        public myStruct(MyOwnType val1, int val2, int val3){ 
            value1 = val1; 
            value2 = val2; 
            value3 = val3; 
        } 
    }
    
    public class MyOwnType
    {
        public int Id { get; set; }
    
        public MyOwnType(int id)
        {
            this.Id = id;
        }
    }
    

    For Contains to work you would need to implement IComparable:

    public class MyOwnType : IComparable
    {
        public int Id { get; set; }
    
        public MyOwnType(int id)
        {
            this.Id = id;
        }
    
        public int CompareTo(object obj)
        {
            if (obj == null) return 1;
    
            MyOwnType other = obj as MyOwnType;
            if (other != null)
                return this.Id.CompareTo(other.Id);
            else
                throw new ArgumentException("Object is not a MyOwnType");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a next code: struct T { public T(int u) { this.U =
Consider the following struct: public struct vip { string email; string name; int category;
The following code is illegal: public struct MyStruct { public MyStruct(int a, int b)
Consider this example: struct Nobody_Expects_The_Spanish_Inquisition{}; int main(){ throw Nobody_Expects_The_Spanish_Inquisition(); } Output shown on Ideone
Consider a piece of code like this: struct B { int c; B() {
consider this simple and pointless code. #include <iostream> struct A { template<int N> void
Consider this code block: struct Animal { public string name = ; // Error
I have defined my struct like this: struct Test { private string assayName; public
Consider this simple example - public class Person { private String name; private Date
consider the following class and struct public class Entity { public IdType Id {get;set;}

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.