top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are Properties in C-sharp and how to use them?

0 votes
451 views
What are Properties in C-sharp and how to use them?
posted Sep 28, 2016 by Sathaybama

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Properties are used to get or set the value of data members of a class.
They are used to implement data security.
A property contains two blocks: (i) get and (ii) set. These blocks works on the basis of calling conventions.
get and set block is implicit calling of grammar language. Property is define public normally to have get and set control.
For each data members we have to create one property.
In c-sharp we create indexers in place of parameterized property. (In VB you can have parameterized property).
For static data member static properties are used.
Difference between Property and Methods

Property
Property is implicitly called using calling convention
Property works on compile and runtime.
Method
Method is explicitly called
Methods works on runtime.
Practical: Showing how to use property

using System;
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Properties_Csharp 
{ 
    class Program 
    { 

        public class PropertyClass 
        { 
            string name; 
            int age; 

            static string co_name; 

            static int rollno; 

            // Static Property 
            public static string _co_name 
            { 
                get 
                { 
                    return co_name; 
                } 

                set 
                { 
                    co_name = value; 
                } 
            } 

            public static int Rollno 
            { 

                set 
                { 
                    rollno = value; 
                } 

                get 
                { 
                    return rollno; 
                } 
            } 


            public string p_name 
            { 

                get 
                { 
                    return name; 
                } 

                set 
                { 
                    name = value; 
                } 
            } 

            public int p_age 
            { 

                get 
                { 
                    return age; 
                } 

                set 
                { 
                    age = value; 
                } 
            } 

        } 

        static void Main(string[] args) 
        { 

            PropertyClass ob1 = new PropertyClass(); 

            PropertyClass.Rollno = 90; 
            Console.WriteLine(PropertyClass.Rollno); 

            PropertyClass._co_name = "George"; 

            string compnay = PropertyClass._co_name; 
            Console.WriteLine(PropertyClass._co_name); 

            ob1.p_age = 23; 
            ob1.p_name = "Peter"; 
            Console.WriteLine(ob1.p_age); 
            Console.WriteLine(ob1.p_name); 

            Console.ReadLine(); 


        } 
    } 
}
answer Sep 28, 2016 by Shivaranjini
...