C# – the best way to define semi-constant class variables in C#

c++naming-conventions

I have a class called ItemBase from which a number of classes inherit.

In this class I have a number of one-line methods which simply return a string or object and are available to all the inheriting classes. I use these quite a bit in inherited classes to make code more readable and so that I always have one and only one place that defines each calculation or piece of information.

I used Getters like I do in PHP and Java since this is the way I have always done it:

public class ItemBase
{
    protected string GetApplicationPath()
    {
        return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
    }

    protected string GetApplicationDataPath()
    {
        return GetApplicationPath() + @"..\..\..\TestServices\";
    }
}

However, I've noticed that Getters and Setters aren't really used in C# as they are in Java or PHP and and I suspect that there is a better, more succinct/standard way to do this in C#.

But I don't want to use class variables since it doesn't seem right to define logic in the a class variable initializer, I assume that even though possible for some calculations it is probably frowned upon and causes problems in some cases:

protected string _applicationPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);

I stay away from public fields after reading about disadvantages you have with them down the road in Jon Skeet's article. Plus this is too much code for just a simple definition.

protected string ApplicationPath
{
    get
    {
        return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
    }
}

I could use C#'s abbreviated properties but then each little piece of information has code in two places and the name is used twice, just doesn't seem like a succinct solution.

protected string ApplicationPath { get; set; }

public ItemBase()
{
    ApplicationPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
}

So I've settled on using these Getters just like I do in PHP and Java, although this doesn't seem to be a very C# way to go about it.

What conventions do you use in C# for storing these "one-line class variables" that just hold a piece of information or do a little calculation?

ANSWER:

protected string ApplicationPath
{
    get { return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase); }
}

protected string ApplicationDataPath
{
    get { return ApplicationPath + @"..\..\..\TestServices\"; }
}

Best Solution

"I could use C#'s abbreviated properties but then each little piece of information has code in two places and the name is used twice, just doesn't seem like a succinct solution"

What do you mean by that? Properties are just syntactic sugar for the getters / setter methods you would normally do in Java. Where do you see code in two places?

public string MyProp
{
   get{return "MyProp";}
}