Method you can only run in a constructor

Viewed 381

Is there a way to mark a method in c# so it can only run in the constructor? Basically, I have a load of read only string arrays that I need to update with the base columns in the constructor so I do the following:

    public CMSBlogContext(ICMSRelationshipContext relationshipContext)
    {
        _relationshipContext = relationshipContext;
        ColumnNames.AddBaseColumns(ref _blogAuthorColumns);
        ColumnNames.AddBaseColumns(ref _blogCardColumns);
        ColumnNames.AddBaseColumns(ref _blogCategoryColumns);
        ColumnNames.AddBaseColumns(ref _blogPageColumns);
        ColumnNames.AddBaseColumns(ref _blogPostColumns);
    }

I thought I could tidy this up by moving the columns into a separate function like so:

    public CMSBlogContext(ICMSRelationshipContext relationshipContext)
    {
        _relationshipContext = relationshipContext;
        AddBaseColumns();
    }

    private void AddBaseColumns() 
    {
        ColumnNames.AddBaseColumns(ref _blogAuthorColumns);
        ColumnNames.AddBaseColumns(ref _blogCardColumns);
        ColumnNames.AddBaseColumns(ref _blogCategoryColumns);
        ColumnNames.AddBaseColumns(ref _blogPageColumns);
        ColumnNames.AddBaseColumns(ref _blogPostColumns);
    }

But obviously this complained that read only can only be updated in the constructor, so is there a way to make a method only able to run in the constructor (or rather, is there a way to put those read only updates into a method not in the constructor but will only run in the constructor)?

1 Answers
Related