Information Technology Reference
In-Depth Information
You can also return objects of user-defined types. For example, the following code returns
an object of type
MyClass
.
Return type -- MyClass
↓
MyClass method3{ )
{
MyClass mc = new MyClass();
...
return mc; // Return a MyClass object.
}
↑
As another example, in the following code, method
GetHour
is called in the
WriteLine
state-
ment in
Main
, and returns an
int
value to that position in the
WriteLine
statement.
class MyClass
{
↓
Return type
public int GetHour()
{
DateTime dt = DateTime.Now; // Get the current date and time.
int hour = dt.Hour; // Get the hour.
return hour; // Return an int.
}
↑
}
Return value
class Program
{
static void Main()
{
Method invocation
MyClass mc = new MyClass();
↓
Console.WriteLine("Hour: {0}", mc.GetHour());
}
↑
↑
}
Instance name
Method name