C# Properties

C# Properties

C# Properties

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

What are Properties?

Lets take look at our previous example when we were talking about methods. In the bellow example you can see that we have used two methods to change user password. Variable password is private so it can be accessed only inside the class User. Changing user password could be done easier using Properties.

public class User
        {
            public User(string fName, string lName)
            {
                firstName = fName;
                lastName = lName;
            }

            public string firstName;
            public string lastName;
            public string email;
            public int age;
            private string password;

            public void SetPassword(string newPassword)
            {
                password = newPassword;
            }

            public string GetPassword()
            {
                return password;
            }
        }

Properties allow you to control the accessibility of a classes variables, and is the recommended way to access variables from the outside in an object oriented programming language like C#. A property is much like a combination of a variable and a method - it can't take any parameters, but you are able to process the value before it's assigned to our returned. A property consists of 2 parts, a get and a set method, wrapped inside the property:

public class User
{
        public User(string fName, string lName)
        {
            firstName = fName;
            lastName = lName;
        }

        public string firstName;
        public string lastName;
        public string email;
        public int age;
        
        private string password;
        /// 
        /// It sets and gets user password
        /// 
        public string Password 
        {
            get
            {
                if (String.IsNullOrEmpty(password))
                    return "Password can not be empty!";
                else
                    return password;
            }
            set
            {
                password = value;
            }
        }
}

The get method should return the variable, while the set method should assign a value to it. Our example is as simple as it gets, but it can be extended. Another thing you should know about properties is the fact that only one method is required - either get or set, the other is optional. This allows you to define read-only and write-only properties. Now I will explain to You have to use properties:

User userInfo = new User("John", "Johnson");
userInfo.Password = "password";
Console.WriteLine("User passowrd is " + userInfo.Password );

You can see because of the set part of the Property we can set the value to our password variable, and because of the get part we can read the value from the variable password. One last note You can also use Properties without variables. Please take a look at a following example:

public string Password 
{
    get;
    set;
}

Posted by Ingenium Llc.

Ingenium Llc.

iNGENIUM Llc. is an software development company from EU which delivers a full range of custom .NET, web and mobile solutions for different business to meet partner's demand.

The Power of Imagination Makes Us Infinite

Related Posts

Comments

comments powered by Disqus