Well I am starting to build some classes for my app. I am just not sure what the best way to go about the class structure. I have:
using System;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// Summary description for Person Class
/// </summary>
public class Person
{
private string _forename;
public string Forename
{
get { return _forename; }
set { _forename = value; }
}
private string _surname;
public string Surname
{
get { return _surname; }
set { _surname = value; }
}
public string GetFullName()
{
var fullname = string.format("{0} {1}", _forename, _surname);
return fullname;
}
public Person()
{
//
// TODO: Add constructor logic here
//
}
}
but I have been reading and what is the difference between the following?
using System;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// Summary description for Person Class
/// </summary>
public class Person
{
public string forename{ get; set; }
public string surname{ get; set; }
public string GetFullName()
{
var fullname = string.format("{0} {1}", forename, surname);
return fullname;
}
public Person()
{
//
// TODO: Add constructor logic here
//
}
}
I guess I want to know which way is the best way to go? Very simple examples
"in C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a (auto-implemented)
property ... the compiler creates a private, anonymous backing field that can only be accessed through the property's
get and set accessors."
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Tuppers
Member
456 Points
236 Posts
Just Starting! Just after some advice and poniting in the right direction
Nov 06, 2012 09:48 PM|LINK
Well I am starting to build some classes for my app. I am just not sure what the best way to go about the class structure. I have:
using System; using System.Collections.Generic; using System.Web; /// <summary> /// Summary description for Person Class /// </summary> public class Person { private string _forename; public string Forename { get { return _forename; } set { _forename = value; } } private string _surname; public string Surname { get { return _surname; } set { _surname = value; } } public string GetFullName() { var fullname = string.format("{0} {1}", _forename, _surname); return fullname; } public Person() { // // TODO: Add constructor logic here // } }but I have been reading and what is the difference between the following?
using System; using System.Collections.Generic; using System.Web; /// <summary> /// Summary description for Person Class /// </summary> public class Person { public string forename{ get; set; } public string surname{ get; set; } public string GetFullName() { var fullname = string.format("{0} {1}", forename, surname); return fullname; } public Person() { // // TODO: Add constructor logic here // } }I guess I want to know which way is the best way to go? Very simple examples
Please help
gerrylowry
All-Star
20577 Points
5721 Posts
Re: Just Starting! Just after some advice and poniting in the right direction
Nov 06, 2012 10:03 PM|LINK
@ Tuppers TIMTOWTDI =. there is more than one way to do it
imho, the best approach is to use Auto-Implemented properties ... i strongly recommend that you take some time to study this MSDN section: http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx "Properties (C# Programming Guide)", especially this sub-topic: http://msdn.microsoft.com/en-us/library/bb384054.aspx "Auto-Implemented Properties (C# Programming Guide)".
"in C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a (auto-implemented) property ... the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors."
g.