C # Little Base & ldquo; Best practices & rdquo; Questions for beginners

advertisements

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


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.