views:

200

answers:

3

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\"; }
}
+2  A: 

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?

After your description: use read-only properties. This is the way to do it in C#, and in most cases it really is the only reasonable alternative. As you've said yourself:

I've noticed that Getters and Setters aren't really used in C# as they are in Java or PHP

Yes, and the reason is simple: .NET's properties offer a special syntactic sugar to express getters and setters. In a way, the latter are just a workaround to compensate for the absence of the former.

Some cases where you might not want to use properties:

  • Compile-time constants that are guaranteed to never, ever, change: use public constants (const) for these:

    public const string Uuid = "D4D5DF8E-51DD-11DE-825A-1A5C55D89593";
    
  • Cases where you want to make it clear for the consumer that the operation is (potentially) time-consuming and/or not free of side-effects: use a Get-prefixed method. However, in the case of side-effects it would probably be better to let the method name reflect that, too.

Konrad Rudolph
I like that rule: use properties when there is no resource use and getters when there is.
Edward Tanguay
+3  A: 

"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";}
}
BFree
Look at the code below the statement about abbreviated properties (by which Edward means automatically implemented properties) - the property name is used in the property declaration and in the constructor.
Jon Skeet
Yea, but from his java style getter method, it doesn't seem that he's using a backing field, which leads me to believe he never has a need to set them. So, why would he need the auto property in the first place?
BFree
ok I think I was confusing read-only properties and public fields. This is what I wanted to know, so your example is a read-only property, it uses the C# syntax sugar, it's one line, easy to read, and safe (in the sense of Jon Skeet's mutable struct example). So that's the answer: one-line, read-only properties as in your example is the best way to implement these in C#.
Edward Tanguay
right I was thinking you had to define the property (protected string ApplicationPath { get; set; }) and then set it e.g. in the constructor...my "public field" example was just confused, that's a property like yours just not condensed to one line.
Edward Tanguay
+1  A: 

The answer is exactly the same as one of the options you had already discounted as being too much code? Except that those property names start with Get, which makes no sense at all..

A protected property with only a get implementation is indeed the way to go though. It should be inlined by the compiler if anyone was wondering.

Thorarin
thanks, I fixed it.
Edward Tanguay