ObjectId id1 = ObjectId.GenerateNewId(); ObjectId id2 = ObjectId.GenerateNewId(); if(id1.Equals(id2)) { Console.WriteLine("The object IDs are equal."); } else { Console.WriteLine("The object IDs are not equal."); } // Output: The object IDs are not equal.
string hexString = "5f98010c3334343762323061"; ObjectId id1 = new ObjectId(hexString); ObjectId id2 = new ObjectId(hexString); if(id1.Equals(id2)) { Console.WriteLine("The object IDs are equal."); } else { Console.WriteLine("The object IDs are not equal."); } // Output: The object IDs are equal.In this example, we create two new ObjectId values with the same hex string value and compare them using the Equals method. Since the two object IDs are identical, the output will indicate that they are equal. The ObjectId Equals method is part of the MongoDB.Bson package library.