Click here to Skip to main content
15,893,381 members
Home / Discussions / C#
   

C#

 
GeneralRe: Select element from a List<> Pin
DPaul19945-Apr-15 10:16
DPaul19945-Apr-15 10:16 
SuggestionRe: Select element from a List<> Pin
Richard Deeming7-Apr-15 2:21
mveRichard Deeming7-Apr-15 2:21 
GeneralRe: Select element from a List<> Pin
DPaul19947-Apr-15 6:43
DPaul19947-Apr-15 6:43 
Questionneed to order a file in stream read and write c# Pin
Member 115700984-Apr-15 7:31
Member 115700984-Apr-15 7:31 
AnswerRe: need to order a file in stream read and write c# Pin
PIEBALDconsult4-Apr-15 8:03
mvePIEBALDconsult4-Apr-15 8:03 
GeneralRe: need to order a file in stream read and write c# Pin
Member 115700984-Apr-15 8:26
Member 115700984-Apr-15 8:26 
QuestionC# class constructor best practice ? Pin
BillWoodruff4-Apr-15 5:05
professionalBillWoodruff4-Apr-15 5:05 
GeneralRe: C# class constructor best practice ? Pin
PIEBALDconsult4-Apr-15 5:52
mvePIEBALDconsult4-Apr-15 5:52 
Of those, I prefer 1, but they all do too much; a constructor should do very little.

Better to have the constructor receive the List rather than build it. If you do, you might want to make a copy.
A benefit of this technique is that it allows the caller to instantiate your class multiple times with the same data without generating it more than once.

And consider using params:
C#
public TheClass(string name, params int[] List)
{
    Name = name;

    TheClassList = new List<int> { List };


The caller can use this with the values he wants:
C#
TheClass a = new TheClass ( "A" , 1 , 2 , 3 ) ;

int[] l = new int[] { 1 , 2 , 3 } ;
TheClass b = new TheClass ( "B" , l ) ;

Basically, generating the list is not the constructor's concern.

Having said that... I'm reminded of a Pool<T> class I wrote. Its constructor takes a delegate that constructs one item for the pool and the number of items it should manage. Maybe I should change it to follow my advice above.
C#
[System.ObsoleteAttribute("Don't use this constructor",true)]
public Pool
(
    int Capacity
,
    Delegate Creator
)
{
    this.pool = new T[Capacity];

    for (int i = 0; i < this.pool.Length; i++)
    {
        this.pool[i] = Creator();
    }

    return;
}

public Pool
(
    params T[] Pool
)
{
    this.pool = Pool;

    return;
}

Modularity, single-responsibility, etc.

modified 4-Apr-15 13:10pm.

GeneralRe: C# class constructor best practice ? Pin
BillWoodruff4-Apr-15 8:21
professionalBillWoodruff4-Apr-15 8:21 
GeneralRe: C# class constructor best practice ? Pin
PIEBALDconsult4-Apr-15 8:27
mvePIEBALDconsult4-Apr-15 8:27 
AnswerRe: C# class constructor best practice ? Pin
Eddy Vluggen4-Apr-15 6:47
professionalEddy Vluggen4-Apr-15 6:47 
GeneralRe: C# class constructor best practice ? Pin
BillWoodruff4-Apr-15 8:28
professionalBillWoodruff4-Apr-15 8:28 
GeneralRe: C# class constructor best practice ? Pin
PIEBALDconsult4-Apr-15 8:50
mvePIEBALDconsult4-Apr-15 8:50 
GeneralRe: C# class constructor best practice ? Pin
Eddy Vluggen4-Apr-15 11:13
professionalEddy Vluggen4-Apr-15 11:13 
GeneralRe: C# class constructor best practice ? Pin
BillWoodruff4-Apr-15 21:42
professionalBillWoodruff4-Apr-15 21:42 
GeneralRe: C# class constructor best practice ? Pin
Eddy Vluggen5-Apr-15 13:13
professionalEddy Vluggen5-Apr-15 13:13 
QuestionIs there any solution to design report instead of Crystal? Pin
aahamdan4-Apr-15 4:14
aahamdan4-Apr-15 4:14 
AnswerRe: Is there any solution to design report instead of Crystal? Pin
OriginalGriff4-Apr-15 4:44
mveOriginalGriff4-Apr-15 4:44 
AnswerRe: Is there any solution to design report instead of Crystal? Pin
tecnova14-Apr-15 4:58
professionaltecnova14-Apr-15 4:58 
GeneralRe: Is there any solution to design report instead of Crystal? Pin
aahamdan4-Apr-15 10:14
aahamdan4-Apr-15 10:14 
GeneralRe: Is there any solution to design report instead of Crystal? Pin
tecnova18-Apr-15 14:54
professionaltecnova18-Apr-15 14:54 
AnswerRe: Is there any solution to design report instead of Crystal? Pin
Eddy Vluggen4-Apr-15 6:50
professionalEddy Vluggen4-Apr-15 6:50 
GeneralRe: Is there any solution to design report instead of Crystal? Pin
Mycroft Holmes5-Apr-15 14:35
professionalMycroft Holmes5-Apr-15 14:35 
GeneralRe: Is there any solution to design report instead of Crystal? Pin
Eddy Vluggen6-Apr-15 0:33
professionalEddy Vluggen6-Apr-15 0:33 
QuestionBest way to Sync data Pin
Jassim Rahma3-Apr-15 11:55
Jassim Rahma3-Apr-15 11:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.