简体   繁体   中英

C# Class Function Return Class (Performance)

I have created a class like this.

public class SimpleClass
{
    public string myProp { get; set; }

    public SimpleClass()
    {
        this.myProp = "";
    }

    public SimpleClass Method1()
    {
        this.myProp += "Method1";
        return this;
    }

    public SimpleClass Method2()
    {
        this.myProp += "Method2";
        return this;
    }

    public string GetProp()
    {
        return this.myProp;
    }
}

I'm using it like this.

public class Worker
{
    public Worker()
    {
        string Output = new SimpleClass().Method1().Method2().GetProp();
    }
}

All functions return the container class and last method returns result.

I'm curious about this performance, is it bad thing to use methods like for performance or good?

Should i use it like that or could you suggesst another way.

Thanks

some suggestion : how should user know First Call method1 then Method2 and finally GetProp() ?

It's better to encapsulate your methods and hide all Complexity. For example User just call GetProp() and in GetProp() you can Do what you want . your exmple can change like below :

 public class SimpleClass
{
    public string myProp { get; set; }

    public SimpleClass()
    {
        this.myProp = "";
    }

    private string Method1()
    {
        this.myProp += "Method1";
        return Method2();

    }

    private string Method2()
    {
      return  this.myProp += "Method2";

    }

    public string GetProp()
    {
        Method1();
        return this.myProp;
    }
}

Finally call your Prop() Method like :

        SimpleClass simple = new SimpleClass();
    string Output = simple.GetProp();

And Another suggestion to have better Design is Make Your Mathod1 and Method2 as Private .

I think that you are reinventing the wheel in wrong way. you are probably looking for StringBuilder which does exactly same thing.

var builder = new StringBuilder();
var result = builder.Append("something").Append("something else").ToString();

but if you still want to have dedicated class to provide meaningful methods instead of just Append and also provide some abstraction over arguments being passed you can do this.

public class SimpleClass
{
    private readonly StringBuilder _builder = new StringBuilder();

    public SimpleClass Method1()
    {
        _builder.Append("Method1");
        return this;
    }

    public SimpleClass Method2()
    {
        _builder.Append("Method2");
        return this;
    }

    public string GetProp()
    {
        return _builder.ToString();
    }
}

Note that using StringBuilder is efficient way of appending strings together. for small number of appends it may not show difference, but for large number of appends it will be faster and produces less garbage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM