Is this class + constructor definition pattern overly redundant?

Posted by Protector one on Stack Overflow See other posts from Stack Overflow or by Protector one
Published on 2010-12-24T12:48:26Z Indexed on 2010/12/24 14:54 UTC
Read the original article Hit count: 259

Filed under:
|

I often come across a pattern similar to this:

class Person {
    public string firstName, lastName;
    public Person(string firstName, string lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

This feels overly redundant (I imagine typing "firstName" once, instead of thrice could be enough…), but I can't think of a proper alternative. Any ideas? Maybe I just don't know about a certain design pattern I should be using here?

Edit - I think I need to elaborate a little. I'm not asking how to make the example code "better", but rather, "shorter". In its current state, all member names appear 3 times (declaration, initialization, constructor arguments), and it feels rather redundant. So I'm wondering if there is a pattern (or semantic sugar) to get (roughly) the same behavior, but with less bloat.
I apologize for being unclear initially.

© Stack Overflow or respective owner

Related posts about oop

Related posts about design-patterns