Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Experts,

I just want to know the difference between public variable and public property of a class.
Posted
Updated 12-Dec-14 16:23pm
v2
Comments
Sergey Alexandrovich Kryukov 13-Dec-14 0:34am    
It is not related to ASP.NET. What's wrong with just reading the basic documentation on .NET and/or C#? Don't try to do development before you read on all the fundamentals.
—SA
argeraju 27-Oct-15 3:56am    
I just wanted to know more differences between them.That is it. Don't take it into wrong dear.
Sergey Alexandrovich Kryukov 27-Oct-15 9:06am    
Who takes something wrong. Is it something about ASP.NET? No. Does the concept of "difference" make any sense? No. What does it mean, "know more differences" (or less differences :-)? You just need to know the concepts and how they work. You got enough useful information already.
—SA

properties having getters and setters which can change in future without effecting to all your other code base. But you can't do the same with variable.
also you can make read only etc by using properties but can't do it with public variable
 
Share this answer
 
v2
In C#, by default all the properties defined in a class are always public.Properties get and set values. The C# language provides them as a convenient way to simplify syntax. They are implemented as methods in the intermediate language. They are standard access points to a class from external code.
Eg:
C#
public class clsMyClass
{
public string myproperty1{get; set;}

}
is an example of a automatic property type available in C#.

Now the 'Question' arises what are major benefits of using Properties in C# over Public variables.

* Public variables are unsafe as everyone can set any value and you have no control over them, while in case of property you can put a check that no one could set invalid value to them.

* Using Properties we can restrict any one, that means he/she can only get the value or read the value but could not set the value from outside of class and vice- a- versa.

* You can not debug with public variables but you can do that with property.

* A variable/field is an implementation and by exposing it directly you are 'breaking encapsulation'.The object now has no way to know its state has changed and that's a bad thing.

* 'Databinding' doesn't acknowledge fields at all, it will only work with properties.

* Properties do not have to refer to an 'actual variable'.
Eg:
C#
 public class clsExample
{
private int _num1;
private int _num2;
public int Sum
{get{return _num1+_num2};}
}

* A property can be a interface member while a variable can't be a member.

* A variable can be passed by reference while a property can't be.

* A variable has only one access modifier while a property have different modifiers on get and set.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 27-Oct-15 9:15am    
So many false points here, starting from the first statement. "By default defined in class" is just gibberish. There cannot be a default in this. You defined either class or structure, no "default", and in both cases you can have properties with equal success.

You may think that I'm picky on your phrasing, but this would be wrong. I'm picking on your understanding, which is badly defective. Not to worry, just keep learning.

—SA
There are no "major" and not major "differences", and the whole concept of "difference" makes no sense. It's just a waste of time and the way to miss the essence of things.

Property is the way to provide the syntax of the field, that is, reading and assigning value, while providing a mechanism to define a side effect behind these operations, both of them, one of them, or none of then. In latter case, you get the way to add such effect without changing the type interface.

Everything else is just the consequence of this. You are trying to throw everything in one pile, which is a sign of lack of understanding and wrong way of thinking.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900