Ali Naeimi
Ali Naeimi

Reputation: 622

Call property after method in c#

is it possible to call properties after methods?

Example:

public static class Test
{
    public static string Show { get; set; }

    public static void Get(string s)
    {
        Show = s;
    }
}

and call like this:

Test.Get("HI").Show;

Upvotes: 0

Views: 617

Answers (1)

Update 2: A fluent interface

If you are implementing a fluent interface, I would suggest renaming your methods as such:

public class Test
{
    public string Show { get; set; }

    public Test ConfigureShow(string show)
    {
        Show = show;
        return this;
    }
}

Your method is much more readable now and clearly defines the intent of the method:

var test = new Test()
    .ConfigureShow("HI");

Make the rest of the methods in your class follow the same pattern and chain your methods on new lines for improved readability.

Update 1: What is your intent?

Is there a particular reason that you are trying to do this? There are several issues with your class:

  • Your methods should make sense - you should not have a method called Get which takes a parameter and then modifies the object. Generally Get methods imply that you are simply fetching something from the object.
  • Why are you passing in a parameter to the method, only to fetch it again?
  • Are you trying to implement a Fluent interface?

What is wrong with:

var show = "HI";
var test = new Test();
test.Show = show;
//You already have your text in the variable show, so why do you want to access it again?

Original answer to OP's question

public class Test
{
    public string Show { get; set; }

    public Test Get(string s)
    {
        Show = s;
        return this;
    }
}

Upvotes: 3

Related Questions