Automatic properties

In this chapter you will learn:

  1. What is automatic properties and how to use automatic properties in C#
  2. How to use the automatic properties

Automatic property

The get and set accessors' logic can be added by C# compiler. This kind of property is called Automatic property.

Compare the two versions of Rectangle class

class Rectangle{//from  j  a  va  2s .c o m
   private int width;
   
   public int Width{
      get{
         return width;
      }
      set{
         width = value;
      }
   }
}

The following code creates an automatic property.

class Rectangle{
   public int Width {get; set;}
}

C# will generate the backend private field which is used to hold the value of width.

Access automatic properties

The following code creates and accesses the automatic properties.

using System;//from  j  av a2s.c  o  m

public class MainClass
{
    static string MyStaticProperty
    {
        get;
        set;
    }

    static int MyStaticIntProperty
    {
        get;
        set;
    }

    static void Main(string[] args)
    {
        // Write out the default values. 
        Console.WriteLine("Default property values");
        Console.WriteLine("Default string property value: {0}", MyStaticProperty);
        Console.WriteLine("Default int property value: {0}", MyStaticIntProperty);

        // Set the property values. 
        MyStaticProperty = "Hello, World";
        MyStaticIntProperty = 32;

        // Write out the changed values. 
        Console.WriteLine("\nProperty values");
        Console.WriteLine("String property value: {0}", MyStaticProperty);
        Console.WriteLine("Int property value: {0}", MyStaticIntProperty);

    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to create read only or write only property in C#
  2. Combine readonly and read only property
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances