tags:

views:

82

answers:

4

Suppose that we have a class called class1.

The class1 has a method called method1 and this method gets an object of type class1. like this:

public class class1
{
     //instance members
     // property methods

    public void method1(class1 obj)
    {
         //...........
    }
}

What does it mean: the method gets an object of this class type? In what scenarios can this be used?

+2  A: 

It allows method1 to operate on an outside instance of class1.

Zach Johnson
What does your comment exactly mean?
odiseh
@odiseh: It allows you to 'do' something with an instance outside of the instance the method is invoked on. For example, you could compare the `this` instance with the provided instance.
Zach Johnson
@Downvoter: Please explain why you downvoted this answer.
Zach Johnson
+2  A: 

It takes an object of type 'Class1'.

For example, you could do:

Class1 myClass = new Class1();
Class1 yourClass = new Class1();
myClass.method1(yourClass);

Each variable we declared of type Class1 is its own object, with its own functions and members.

Zachary
So you wanna to tell that whenever we want to compare (for example) two objects, then we should have a method inside of our class that gets an object of that type. Yes?
odiseh
Yeah, I'm not an expert on C#, but I believe you implement the 'equals' method, which would take an object of the same type as that class, and check their members to see if they are equal. But you wouldn't just use it to check equality - there are endless reasons why you would want to have a class with methods that accept the same class type in their parameters.
Zachary
@odiseh: That is a common pattern for equality, yes, but you have to be very careful; getting equality right in C# is tricky. If you specifically want to implement a type which has value equality semantics then you should ask a new question about that, or search the archives. There are subtle issues involving the differences between "Equals" and "==", and also issues involving hash code computation.
Eric Lippert
Zachary: the Equals method takes object, not the declaring type. Sure, the first thing Equals usually does is check whether the actual argument is the same type as the declaring type. But in terms of the method declaration, Equals is *not* an example of odiseh's "method that takes an argument of the declaring type" concern.
itowlson
@itowlson: Yes, but a common pattern for implementing Equals is to make an overload that takes the relevant type. I usually implement Equals as something like: public bool Equals(Lobster lobster){ if (lobster == null) return false; ...whatever...} public override bool Equals(object ob) { return Equals(ob as Lobster); }
Eric Lippert
+8  A: 

What does it mean: the method gets an object of this class type?

Yep. Nothing odd about that. Why do you ask?

This sort of thing happens all the time. A Set has a method Union which takes another Set. A Giraffe has a method Mate which takes another Giraffe. A Lobster has a method Eat which takes another Lobster. A sequence has a method Concatenate which takes another sequence. And so on.

Eric Lippert
Somewhere out there someone is firing up Reflector searching for Giraffe.Mate(Giraffe). :)
Josh Einstein
@Josh: It's part of the Common Mammal Class Library. Which is odd, since giraffes aren't that common.
Eric Lippert
I just want to know whether the parameter is covariant or contravariant. I'm not sure which is more ghastly.
itowlson
@itowlson: virtual method inheritance in C# is neither covariant in return type nor contravariant in formal parameter type, so your giraffes are safe for now.
Eric Lippert
Would Lobster.Eat check that !ReferenceEquals(this, other) first? Or are they really not that smart?
Josh Einstein
+3  A: 

Most obvious example I can think of:

public class Node
{
    private m_childNodes List<Node>;
    // ...
    public AppendChild(Node child)
    {
        m_childNodes.Add(child);
    }

}
egrunin