Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return class from function in c#

Tags:

c#

class

Can I return class object from function in c# as a return value instead return string or int or ..etc

like image 463
kartal Avatar asked Nov 16 '10 03:11

kartal


People also ask

How do you return an object from a function?

A function can also return objects either by value or by reference. When an object is returned by value from a function, a temporary object is created within the function, which holds the return value. This value is further assigned to another object in the calling function.

Can a function return a class C++?

Passing and Returning Objects in C++ In C++ we can pass class's objects as arguments and also return them from a function the same way we pass and return other variables. No special keyword or header file is required to do so.

Can the return type of a function be a class?

Absolutely, this is possible.

Can a class return an object?

Yes it can. In fact, that's exactly what a singleton class does. The first time you call its class-level getInstance() method, it constructs an instance of itself and returns that. Then subsequent calls to getInstance() return the already-constructed instance.


1 Answers

A class object in C# is a Type. So you can definitely return it from a function:

public Type Foo() {
    return typeof(string);
}

public Type Bar() {
    return someNonNullVariable.GetType();
}
like image 113
James Kovacs Avatar answered Nov 04 '22 20:11

James Kovacs