Java vs. C#

Properties


Java
 
//traditional property design
public class PropertyHolder
{
    private int someProperty = 0;

    public int getSomeProperty()
    {
        return someProperty;
    }

    public void setSomeProperty(int propValue)
    {
        someProperty = propValue;
    }

}

public class PropertyTester
{
    public static void main(String[] args)
    {
        PropertyHolder propHold = new PropertyHolder();

        propHold.setSomeProperty(5);

        System.out.println("Property Value: " + propHold.getSomeProperty());

        
    }
} 

//read & write property public class PropertyHolder { private int someProperty = 0; public int getSomeProperty() { return someProperty; } public void setSomeProperty(int propValue) { someProperty = propValue; } } public class PropertyTester { public static void main(String[] args) { PropertyHolder propHold = new PropertyHolder(); propHold.setSomeProperty(5); System.out.println("Property Value: " + propHold.getSomeProperty()); } }
//read only property public class PropertyHolder { private int someProperty = 0; public PropertyHolder(int propVal) { someProperty = propVal; } public int getSomeProperty { return someProperty; } } public class PropertyTester { public static void main(String[] args) { PropertyHolder propHold = new PropertyHolder(5); System.out.println("Property Value: "+propHold.getSomeProperty()); } }
//write only property public class PropertyHolder { private int someProperty = 0; public int setSomeProperty(int value) { someProperty = value; } } public class PropertyTester { public static void Main(String[] args) { PropertyHolder propHold = new PropertyHolder(); propHold.setSomeProperty(5); } }

C#
 
//tranditional property design
public class PropertyHolder
{
    private int someProperty = 0;

    public int getSomeProperty()
    {
        return someProperty;
    }

    public void setSomeProperty(int propValue)
    {
        someProperty = propValue;
    }

}

public class PropertyTester
{
    public static int Main(string[] args)
    {
        PropertyHolder propHold = new PropertyHolder();

        propHold.setSomeProperty(5);

        Console.WriteLine("Property Value: {0}", propHold.getSomeProperty());

        return 0;
    }
} 

//new way to design a property. Read & Write using System; public class PropertyHolder { private int someProperty = 0; public int SomeProperty { get { return someProperty; } set { someProperty = value; } } } public class PropertyTester { public static int Main(string[] args) { PropertyHolder propHold = new PropertyHolder(); propHold.SomeProperty = 5; Console.WriteLine("Property Value: {0}", propHold.SomeProperty); return 0; } } Note: To the client, a property looks like a member variable, but to the implementor of the class it looks like a method. It allows you total encapsulation and data hiding while giving your clients easy access to the members. The value is implicitly available to the property.
//read only property using System; public class PropertyHolder { private int someProperty = 0; public PropertyHolder(int propVal) { someProperty = propVal; } public int SomeProperty { get { return someProperty; } } } public class PropertyTester { public static int Main(string[] args) { PropertyHolder propHold = new PropertyHolder(5); Console.WriteLine("Property Value: {0}", propHold.SomeProperty); return 0; } }
//write only property public class PropertyHolder { private int someProperty = 0; public int SomeProperty { set { someProperty = value; } } } public class PropertyTester { public static int Main(string[] args) { PropertyHolder propHold = new PropertyHolder(); propHold.SomeProperty = 5; return 0; } }