-3

1) What is the difference between these 2 lines? I can access them the same way:

 public string ShortName;
    //and
 public string ShortName { get; set; }

2) When do you use in c# getters like java and when do you use like c#. I saw that the c# itselfe use the java style, like GetType

//Like Java:
public string GetShortName(){
  return _shortName
}
//Like c#:
public string ShortName { get { return _shortName; } set;}

3) What is the common convention to name your private members?

private string _shortName; //or private string shortName;


4) What is the common convention to name your constants?

 public const string SHORT_NAME; 
 //or
 public const string ShortName;

5) In what cases do you choose put more than one class in one .cs file?


6) Do you name your namespaces different than your folder structure?

Thanks

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
SexyMF
  • 10,657
  • 33
  • 102
  • 206
  • Is this a question for other people to leave their questions for beginners or are you asking for feedback on the questions you have asked? – bobwah Oct 27 '11 at 07:57
  • 1
    Have you tried search? It is nice feature! – Renatas M. Oct 27 '11 at 07:59
  • This is fairly well answered in a number of places, e.g. http://msdn.microsoft.com/en-us/library/czefa0ke(vs.71).aspx – Paddy Oct 27 '11 at 08:00
  • These should be separate questions. – Michael J. Barber Oct 27 '11 at 08:01
  • 1
    #1 the first line is declaring a variable of type string named ShortName, the second line is is an automatic property named ShortName. #2 the first line is not valid c# syntax, the second line is defining a property (read-only). #3, 4, 5 and 6 are all very subjective. – Tim Oct 27 '11 at 08:01
  • Why must there always be a "smart" guy... its simple questions... – SexyMF Oct 27 '11 at 08:01
  • 3, 4, 5, 6 are all subjective. – Firedragon Oct 27 '11 at 08:05
  • Minor correction to my earlier comment - the first line in #2 *is* a valid syntax for C#. @Hans explains why its not needed in his answer below. – Tim Oct 27 '11 at 08:06
  • 1
    I can't tell without reading this in full if it's some kind of pop quiz for *us* or you're genuinely asking because *you* don't know. Closed. – BoltClock Oct 27 '11 at 08:08
  • Typed out an answer but it got closed too quickly. #3 is subjective. #4 is only subjective if people don't follow MS's prescribed guidelines ([make constants CamelCased](http://stackoverflow.com/questions/242534/c-sharp-naming-convention-for-constants)). #5 is not subjective - multiple classes in one file is a bad habit, not a "different convention" (except for nested classes, which should be avoided except in specific situations, which are rarer these days) #6 is similarly not subjective - you should follow your folder structure with respect to the global namespace defined for your project. – Merlyn Morgan-Graham Oct 27 '11 at 08:15

2 Answers2

1

1.) and 2.) The cool thing about properties is that you can do nice things in the accessors. E.g.

private string name;
public String Name{
    get{
        if(this.name == String.Empty)
            return "No name given";
        else
            return name;
    }
}

This gets more interesting when the member is not a value type but some class. So while a properties gives you the control of a function it can be used like a public member. Also you can make the set accessor internal, protected or even privat.

3.), 4.) and 6.) There are many naming conventions. See for Microsoft's

5.) Don't. This will get you in trouble in the long run. One exception to this are nested (privat) classes.

Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
0

1) The first is a field, the second is an (auto implemented) property. Best practice is to use properties to expose values, not fields.

2) Properties implement getters and setters, you don't have to write javastyle getter and setter methods and pretend they belong together.

3) and 4) both are used.

5) when you want to find the class again, best is to stick to one class per file.

6) usually not.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111