Skip to main content
added 259 characters in body
Source Link
Jodrell
  • 146
  • 6

As of c# 6.0, which is now released, you should just do this,

public class Room
{
    public Room(Client client)
    {
        this.Client = client;
    }

    public Client Client { get; }
}

public class Client
{
    public Client(long id)
    {
        this.Id = id;
    }

    public long Id { get; }
}

Raising a property of Client to Room is an obvious breach of encapsulation and the DRY principle.

If you need to access the Id of a Client of a Room you can do,

var clientId = room.Client?.Id;

Note the use of the Null Conditional Operator, clientId will be an long?, if Client is null, clientId will be null, otherwise clientId will have the value of Client.Id.

As of c# 6.0, you should just do this,

public class Room
{
    public Room(Client client)
    {
        this.Client = client;
    }

    public Client Client { get; }
}

public class Client
{
    public Client(long id)
    {
        this.Id = id;
    }

    public long Id { get; }
}

Raising a property of Client to Room is an obvious breach of encapsulation.

If you need to access the Id of a Client of a Room you can do,

var clientId = room.Client?.Id;

As of c# 6.0, which is now released, you should just do this,

public class Room
{
    public Room(Client client)
    {
        this.Client = client;
    }

    public Client Client { get; }
}

public class Client
{
    public Client(long id)
    {
        this.Id = id;
    }

    public long Id { get; }
}

Raising a property of Client to Room is an obvious breach of encapsulation and the DRY principle.

If you need to access the Id of a Client of a Room you can do,

var clientId = room.Client?.Id;

Note the use of the Null Conditional Operator, clientId will be an long?, if Client is null, clientId will be null, otherwise clientId will have the value of Client.Id.

deleted 11 characters in body
Source Link
Jodrell
  • 146
  • 6

As of c# 6.0, you should just do this,

public class Room
{
    public Room(Client client)
    {
        this.Client = client;
    }

    public Client Client { get; } = client;
}

public class Client
{
    public Client(long id)
    {
        this.Id = id;
    }

    public long Id { get; };
}

Raising a property of Client to Room is an obvious breach of encapsulation.

If you need to access the Id of a Client of a Room you can do,

var clientId = room.Client?.Id;

As of c# 6.0, you should just do this,

public class Room
{
    public Room(Client client)
    {
        this.Client = client;
    }

    public Client Client { get; } = client;
}

public class Client
{
    public Client(long id)
    {
        this.Id = id;
    }

    public long Id { get; };
}

Raising a property of Client to Room is an obvious breach of encapsulation.

As of c# 6.0, you should just do this,

public class Room
{
    public Room(Client client)
    {
        this.Client = client;
    }

    public Client Client { get; }
}

public class Client
{
    public Client(long id)
    {
        this.Id = id;
    }

    public long Id { get; }
}

Raising a property of Client to Room is an obvious breach of encapsulation.

If you need to access the Id of a Client of a Room you can do,

var clientId = room.Client?.Id;
Source Link
Jodrell
  • 146
  • 6

As of c# 6.0, you should just do this,

public class Room
{
    public Room(Client client)
    {
        this.Client = client;
    }

    public Client Client { get; } = client;
}

public class Client
{
    public Client(long id)
    {
        this.Id = id;
    }

    public long Id { get; };
}

Raising a property of Client to Room is an obvious breach of encapsulation.