IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

January Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#OOP   Print This     
  From the January 2016 Issue of Prestwood eMag
 
C# OOP:
C# Member Property (no (), get, set)
 
Posted 11 years ago on 12/20/2008 and updated 2/3/2009
C# Code Snippet:
 A flashcard from our C# Flashcards Library
 A code snippet from our C# Code Snippets Page
 Tags: C# , Member Property

KB101738



C# Member Property

In C#, parens indicate a method and the lack of parens indicate a property. You use special get and set methods to both get and set the values of properties.

C# 3.0 introduced auto-implemented properties for use when no additional logic is required.

pulic int VendorID {get; set;}

For a read-only property, leave out the set method.

The value keyword is used to refer to the member field. Properties can make use of any of the access modifiers (private, protected, etc). It is common to use a lowercase member names for member fields ("name" in our example) and uppercase properties to manage member fields ("Name" in our example).

Syntax Example:
public class Cyborg : System.Object
{
  private string cyborgName;
 
  public string CyborgName
  {
  get {return cyborgName;}
  set {cyborgName = value;}
  }

}

Visual C# 2008 Working Winforms Example

The following example demonstrates implementing a very simple class property. The class is named Cyborg which includes one property named CyborgName.

Create a form and place a button on it and add code as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
  
namespace CR_Properties
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
      Cyborg MyRobot = new Cyborg();
      MyRobot.CyborgName = "John";
      MessageBox.Show("Hi, my name is " + MyRobot.CyborgName + ".");
    }
  }
  
  public class Cyborg : System.Object
  {
    private string cyborgName;
  
    public string CyborgName 
    {
      get
      {
        return cyborgName;
      }
     
      set
      {
        cyborgName = value;
      }
    }
  }
}

Referencing Properties within the Class and from an Object Instance

From within the class code (and descendant classes), you can use a property by using the property name (you cannot use class dot notation), or you can also refer directly to the member variable if it's within scope. In our example above, you can use either the property or member field within the class (CyborgName) or the member field (cyborgName). You can use the private member field within the class because that is the definition of private visibility. However, you can only use the property CyborgName in descendant classes. You cannot use ClassName.PropertyName.

For object instances, you use ObjectInstance.PropertyName as in MyRobot.CyborgName.

For example, you can add the following method to our Cyborg class above:

public virtual void IntroduceYourself()
{
if (CyborgName == null)
MessageBox.Show("Hi, I do not have a name yet.");
else
MessageBox.Show("Hi, my name is " + CyborgName + ".");
}

 

Then you can add two buttons to the form and consume this new method as follows:

private void button2_Click(object sender, EventArgs e)
{
  Cyborg MyRobot = new Cyborg();
  MyRobot.IntroduceYourself();
}
  
private void button3_Click(object sender, EventArgs e)
{
  Cyborg MyRobot = new Cyborg();
  MyRobot.CyborgName = "Cameron";
  MyRobot.IntroduceYourself();
}

 

More Info


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1164A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Intermediate

1 Intermediate Level Question

Question #1: Multiple Choice

Given the following two code snippets:

//Code Snippet 1
public class Cyborg : System.Object
{
  private string cyborgName;
 
  public string CyborgName
  {
  get {return cyborgName;}
  set {cyborgName = value;}
  }
}
  
//Code Snippet 2
public class Cyborg : System.Object
{
  public string CyborgName {get; set;}
}
Answer:
1. 

Both are syntactically correct but accomplish completely different tasks.

2. 

Both are syntactically correct and accomplish the equivalent task.

3. 

Code snippet 1 is syntactically correct but code snippet 2 is not.

4. 

Code snippet 2 is syntactically correct but code snippet 1 is not.

Advanced

1 Advanced Level Question

Question #2: True or False?

You can set the initial value of C# member property as follows:

pulic int VendorID = -1 {get; set;}
Answer:
  • True
  • False

  •  KB Article #101738 Counter
    15278
    Since 12/20/2008
    -
       Contact Us!
     
    Have a question? Need our services? Contact us now.
    --Mike Prestwood

    Call: 916-726-5675

    email: info@prestwood.com


    Go ahead!   Use Us! Call: 916-726-5675 


    ©1995-2019 Prestwood IT Solutions.   [Security & Privacy]