Re: automatic code generation for properties

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 02/04/04


Date: Wed, 4 Feb 2004 13:42:33 -0000

Abhishek Srivastava <abhishek-srivastava@nospam.net> wrote:
> There is a particular feature which I want to have in visual studio .net
>
> suppose I create a class
>
> ClassA
> {
> private string name;
> private int age;
> }
>
> Then I right click on my code and say "generate properties".
>
> And a property gets created (with capital for the first character ex.
> Name, Age) for each of the private members of my class.
>
> Can I do this is vs.net?

Others have answered the question you've actually asked, but I'd like
to propose a different solution: namely a way of the C# language itself
changing to allow "simple" property definitions. The syntax is up for
grabs, but I'd suggest something like:

<AccessModifier> property <TypeName> <fieldName> [ = <initialValue>]
<PropertyName> {[get;][set;]}

For instance:

public property string name Name { get; }
protected property int height=10 Height {get; set; }

would be exactly equivalent to:

private string name;
public string Name
{
    get { return name; }
}

private int height=10;
protected int Height
{
    get { return height; }
    set { height = value; }
}

XML documentation applied to the short form would either be applied to
the field, the property or both - up for grabs, but I'd favour the
property, with an automatic XML documentation for the field of "Field
backing the simple property <PropertyName>: <XML doc for PropertyName>"

I was seriously anti this a while ago, and argued fairly vehemently
against it - but I now believe it would make a lot of code
significantly simpler, and might persuade people to use properties
where they might otherwise use public fields just for convenience.

Whether or not one could declare the property to be virtual, I don't
know - and likewise exactly how different access modifiers for get/set
would work.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Getting Error in Login() method in FtpConnection Class
    ... private static int BUFFER_SIZE = 512; ... private static Encoding ASCII = Encoding.ASCII; ... private string server = "localhost"; ... public void Login() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem with FTP
    ... > private static Encoding ASCII = Encoding.ASCII; ... > private string message = null; ... > private int port = 21; ... > public void Login() ...
    (microsoft.public.pocketpc.developer)
  • Versioning question
    ... bool SaveActivity(Activity a, string username, string password); ... ActivityTypeID, DateTime FollowUpDate, int TimeSpent, bool isOpen, ... private DateTime _FollowUpDate; ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Resizing Panel Problem
    ... Mark Arteaga, MVP ... >>> private void frmViewSchedule_Load ... >>> private int intX; ... >>> private string strFullDetails; ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Resizing Panel Problem
    ... Mark Arteaga, MVP ... > private void frmViewSchedule_Load ... > private int intX; ... > private string strFullDetails; ...
    (microsoft.public.dotnet.framework.compactframework)

Loading