Is it possible to create an immutable object without passing everything in the constructor?

I want to make my class immutable. The obvious way would be to declare all fields as get; private set;and initialize all fields in the constructor. Therefore, customers must provide everything in the constructor. The problem is that if there are ~ 10 or more fields passed in the constructor, it becomes very unreadable, because there are no labels for each field.

For example, this is pretty readable:

info = new StockInfo
        {
            Name = data[0] as string,
            Status = s,
            LotSize = (int)data[1],
            ISIN = data[2] as string,
            MinStep = (decimal)data[3]
        };

compare with this:

new StockInfo(data[0] as string, s, (int) data[1], data[2] as string, (decimal) data[3])

And now the image that I have 10 or more parameters.

So, how can I make an immutable class?

I can suggest using the same formatting when using the constructor:

info = new StockInfo(
            data[0] as string,           // Name
            s,                           // Status
            (int)data[1],                // LotSize
            data[2] as string,           // ISIN
            (decimal)data[3]             // MinStep
       );

Can you offer something better?

+5
source share
6 answers

, . , .

  • .
  • ( ), , .

, , - data[0] as string string stockName = data[0] as string;, stockName. .

, . , .

+6

, #:

var info = new StockInfo
(
    Name: data[0] as string,
    Status: s,
    LotSize: (int)data[1],
    ISIN: data[2] as string,
    MinStep: (decimal)data[3]
);
+8

. , :

( ) . (: . .NET .)

info = new StockInfo
           (
             name: data[0] as string,
             status: s,
             ...
           )

. (: . .)

public interface IStockInfo
{
   string Name { get; }
   string Status { get; }
}

IStockInfo info = new StockInfo
                      {
                          Name = data[0] as string,
                          Status = s,
                          ...
                      }

- ., , ReadOnlyCollection<T>. (: . . .)

var readOnlyInfo = new ReadOnlyStockInfoDecorator(info);

. (: . . .)

var immutableInfo = new ImmutableStockInfo(info);

. (: .)

info.Freeze();
info.Name = "Test"; // Make this throw an exception.

(: . . . )

info = StockInfo.FromName(data[0] as string)
                .WithStatus(s) // Make this create a modified copy
                .WithXXX() ;
+7

, ?

:

info = new StockInfo(
        name: data[0] as string,
        status: s,              
        lotSize: (int)data[1],  
        isin: data[2] as string,
        minStep: (decimal)data[3]
   );

, , . , . , factory.

, , .

+1

:

, . (, ), "" , (, , , ).

, 5 ( , IDE ), . , , , , coördinate.

, , , ( , , - , , ) , , , , - , , t, .

Builder

: StringBuilder UriBuilder. , - , , , .

, , , , , ( , - Append() ), , .

, 30 , 30 , . , , .

0

, " " , AsMutable, AsNewMutable AsImmutable. AsImmutable, . AsImmutable, , , , , , . , AsImmutable .

AsMutable, " " . , " ", AsMutable.

AsNewMutable , AsMutable. AsImmutable.AsMutable, ( , , , ).

, ( , "" "" ), ( , ). , , - ; , , , . , , , , AsImmutable, . AsImmutable , , , .

. , GetHashCode Equals , . , , , , , -, , . , , - double, float Decimal, Object.Equals, , .

0

All Articles