Is it good practice to use Extension methods just to set default properties?

.net-3.5, c#, extension-methods

Solution

Yes you definitely can do it that way. As extension methods are just syntactic sugar for `static` methods, and as you want to build some static helper method to configure a given instance of an object, it makes sense.

Just for the sake of the completeness, control configuration are sometimes done using extension methods associated with a fluent interface:

public static class DataGridExtensions
{
    public static DataGrid AllowColumnConfiguration(this DataGrid grid)
    {
        // Add NRE check
        grid.CanUserResizeColumns = true;
        grid.CanUserSortColumns = true;
        grid.CanUserReorderColumns = true;

        return grid;
    }

    public static DataGrid AllowEdition(this DataGrid grid)
    {
        // Add NRE check
        grid.CanUserAddRows = true;
        grid.CanUserDeleteRows = true;

        return grid;
    }
}

So you can use it that way:

var grid = new DataGrid()
    .AllowColumnConfiguration()
    .AllowEdition();

Problem

I am using a set of 3rd party controls, and I am considering using an extension method to set some properties that are wanted almost anytime this control is used within the application. Is it good practice to build extension methods for this sort of use? And why or why not? For more detail, the 3rd party library is DevExpress, and the extension method I want to write would disable all customization and editing on a specific control of theirs. So instead of writing ``` barManager.AllowCustomization = false; barManager.AllowMoveBarOnToolbar = false; barManager.AllowQuickCustomization = false; barManager.AllowShowToolbarsPopup = false; barManager.AutoSaveInRegistry = false; foreach (Bar bar in barManager.Bars) { bar.OptionsBar.DrawDragBorder = false; } ``` I could write ``` barManager.DisableEditing(); ```

Original source