Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using type object as returning type - bad practice?

Tags:

methods

c#

oop

I have a method

    private object SetGrid(IGrid grid)
    {
        grid.PagerHelper.SetPage(1, 10);
        grid.SortHelper.SetSort(SortOperator.Ascending);
        grid.PagerHelper.RecordsPerPage = 10;

        return grid;
    }

which returns an object of type object.

Then I cast the object back to the previous type.

    var projectModel = new ProjectModel();

    projektyModel = (ProjectModel)SetGrid(projectModel);

The gain of this is, the method SetGrid can be reused across the app.

Is this a common practice or should I avoid doing this ?

like image 873
user137348 Avatar asked Feb 05 '10 09:02

user137348


People also ask

Can an object be a return type?

User-defined functions and class methods can define return types as object references (as class or interface types). When an object is passed locally, class instances are always returned by reference. Thus, only a reference to an object is returned, not the object itself.

Can functions return any type of object?

A function may be defined to return any type of value, except an array type or a function type; these exclusions must be handled by returning a pointer to the array or function. When a function does not return a value, void is the type specifier in the function declaration and definition.

What is meant by returning an object from a method?

It would mean make a copy and return it. The difference is that if you return pointer to objects internal variable that object state could be modified from outside. If you return copy that copy can be modified and the original object will not change.

What is object return type in C#?

In C#, a method can return any type of data including objects. In other words, methods are allowed to return objects without any compile time error.


2 Answers

You could use a generic method instead, and constrain the type argument to your IGrid interface:

private T SetGrid<T>(T grid) where T : IGrid
{
    grid.PagerHelper.SetPage(1, 10);
    grid.SortHelper.SetSort(SortOperator.Ascending);
    grid.PagerHelper.RecordsPerPage = 10;

    return grid;
}

You should still be able to call the method in exactly the same way, just without the cast. Type inferencing should be capable of automagically figuring out the required generic type argument for you:

var projectModel = new ProjectModel();
projektyModel = SetGrid(projectModel);

EDIT...

As other answers have mentioned, if your IGrid objects are reference types then you don't actually need to return anything at all from your method. If you pass a reference type then your method will update the original object, not a copy of it:

var projectModel = new ProjectModel();  // assume that ProjectModel is a ref type
projektyModel = SetGrid(projectModel);
bool sameObject = object.ReferenceEquals(projectModel, projektyModel);  // true
like image 62
LukeH Avatar answered Nov 05 '22 06:11

LukeH


Since you are passing in an object of a class that implements IGrid you could just as well change the return type to IGrid.

Also, since it's a reference type you don't even need to return the grid again. You could just as well use this:

var projectModel = new ProjectModel();
SetGrid(projectModel);
like image 22
BennyM Avatar answered Nov 05 '22 05:11

BennyM