I'm studying C#.
The method in the class has to specify the type of return value.
I know intuitively that I can specify string or int for string type and numeric type.
How do I return an instance?
Also, do you understand that void has no return value?
public class HogeClass{
void Fuga() {
return new Piyo();
}
}
Specify the instance type name (Piyo
) instead of void
.
Piyo Fuga(){
return new Piyo();
}
You can also specify the type as defined by structure, class, interface, and enum.
According to Microsoft's documentation, it is called a custom type.
https://msdn.microsoft.com/ja-jp/library/ms173104.aspx
Therefore, you can define a method that returns Piyo
to the return type of the method.
public class hoge {
Piyo Fuga(){
return new Piyo();
}
}
As you know, void means there is no return value.
If the return value type is void, return only stops processing and does not return the value.
Do you understand that void
has no return value?
That's right.
How do I return an instance?
Specifies the class name itself as the return type.
You can also use this keyword to return the instance on which the method was called.
// Example method to return your own instance
public Hoge GetInstance()
{
return this;
}
This example may seem meaningless because an instance is required to invoke the method, but for example, the StringBuilder
class achieves the method chain by returning the instance with the return value of the Append
method.
varsb=new System.Text.StringBuilder();
Console.WriteLine(sb.Append("Sentence").Append("Connect").Append("Register"").ToString());
If you want to invoke a new instance for a particular class, you can also use the static
method to create it from an instance-free state.
It can be used to hide the actions taken when creating an instance or to create an instance using the interface.
public static Hoge Create()
{
var hoge = new Hoge();
hoge.Name="This instance was created from a static method.";
return hoge;
}
In addition, you create classes for instance generation to hide what happens when an instance is created or to change behavior depending on the class you inherit.
This is called the factory pattern.
There are advantages and disadvantages to each coding method, so please check the details yourself when using it.
Verification Code:
using System;
using System.Collections.General;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class program
{
static void Main (string[]args)
{
var hoge = new Hoge
{
Name="This instance has been initialized on its own.",
};
Console.WriteLine(hoge.Name);
var hoge2 = hoge.GetInstance();
Console.WriteLine(hoge2.Name);
var hoge3 = Hoge.Create();
Console.WriteLine(hoge3.Name);
var factory = new HogeFactory();
varhoge4 = factory.Create();
Console.WriteLine(hoge4.Name);
varsb = new System.Text.StringBuilder();
Console.WriteLine(sb.Append("Sentence").Append("Connect").Append("Register"").ToString());
Console.Read();
}
}
public class Hoge
{
public string name {get;set;}
public Hoge GetInstance()
{
// Return your own instance
return this;
}
public static Hoge Create()
{
var hoge = new Hoge();
hoge.Name="This instance was created from a static method.";
return hoge;
}
}
public class HogeFactory
{
public Hoge Create()
{
var hoge = new Hoge
{
Name="This instance was created in a factory pattern."
};
return hoge;
}
}
}
© 2024 OneMinuteCode. All rights reserved.