> For the complete documentation index, see [llms.txt](https://apwt.gitbook.io/zie-scherp-scherper/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://apwt.gitbook.io/zie-scherp-scherper/h16-polymorfisme/6_equals.md).

# Objecten vergelijken: alles komt samen

Dankzij polymorfisme hebben we nu met de `is` en `as` keywords handige hulpmiddelen om meer "generieke" methoden te schrijven. Herinner je je nog de `Equals` methode die we schreven om 2 studenten te vergelijken toen we leerden dat alle klassen van `System.Object` overerfden? Laten we deze code er nog eens bijnemen en verbeteren:

```csharp
//In de Student klasse
public override bool Equals(Object o)
{  
    Student temp = (Student)o; 
    return (Geboortejaar == temp.Geboortejaar && Voornaam == temp.Voornaam);
}
```

De eerste lijn waarin we `o` casten naar een student kan natuurlijk mislukken. Het is dan ook veiliger om eerst te controleren of we wel mogen casten, voor we het effectief doen. Hierdoor schrijven we een minder foutgevoelige methode:

```csharp
//In de Student klasse
public override bool Equals(Object o)
{  
    if(o is Student)
    { 
        Student temp = o as Student; 
        return (Geboortejaar == temp.Geboortejaar && Voornaam == temp.Voornaam);
    }
    return false;
}
```

Of we kunnen ook het volgende doen:

```csharp
//In de Student klasse
public override bool Equals(Object o)
{  
    Student temp = o as Student; 
    if(temp != null)
    { 
        return (Geboortejaar == temp.Geboortejaar && Voornaam == temp.Voornaam);
    }
    return false;
}
```

Beide zijn geldige oplossingen.

{% hint style="info" %}
De `is` en `as` keywords laten toe om meer dynamische code te schrijven. Mogelijk weet je niet op voorhand wat voor datatype je code zal moeten verwerken en wordt polymorfisme je oplossing. Maar dan? Dan komen `is` en `as` to the rescue!

Je met polymorfisme gevulde lijst van objecten van allerhande typen wordt nu beheersbaarder. Je kan nu met `is` een element bevragen of het van een bepaald type is. Vervolgens kan je met `as` het element tijdelijk 'omzetten' naar z'n effectieve type. Bijgevolg kan dit element dan doen dan wanneer hij kan in de vermomming is van z'n eigen basistype.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://apwt.gitbook.io/zie-scherp-scherper/h16-polymorfisme/6_equals.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
