Hello :)
about the get set shortcut -

public int MyVar { get; set; }

for example
how it different from just:

public int MyVar;

???
i heard that in the shortcut it add private myVar... huh?? if i can't see it how can i use it?

when i use the previous system-

private int myVar;
public int MyVar
{
get {return myVar;}
set {myVar = value;}
}

i could use that to make things happen when i set the variable for example, like:

private int myVar;
public int MyVar
{
get {return myVar;}
set {MakeSomething(); myVar = value;}
}

and now i can't...
maybe i dont understand all the concept of those get/set

please help me understand that!!!
thank u :)

You don't need to access the hidden variable for MyVar.
Just use the public one from inside or outside of your class.

If you need to restrict access to it, either make it private or protected.
Don't worry about it.

You don't need to access the hidden variable for MyVar.
Just use the public one from inside or outside of your class.

If you need to restrict access to it, either make it private or protected.
Don't worry about it.

thanks but i still dont get it... how its different from the public int MyVar;
and if i want to do something when i set or get the variable - how can i do that?

thanks..

I understand what you are asking, but you should realize that {get;set;} accessor methods are the shortcuts to the longer version. You can use the longer version if you need more direct control over what is delivered or you can intercept part of the set or the get.

http://msdn.microsoft.com/en-us/library/aa287786(v=VS.71).aspx

I understand what you are asking, but you should realize that {get;set;} accessor methods are the shortcuts to the longer version. You can use the longer version if you need more direct control over what is delivered or you can intercept part of the set or the get.

http://msdn.microsoft.com/en-us/library/aa287786(v=VS.71).aspx

sorry, i just need to know if i understand that -
so u say that if i dont need to make something happened when the variable is get or set i need to use the shortcut
but when i need to do the "something" while get or set i use the longer version?

if that is right why i need the shortcut?? if i dont need to make things happend in the get/set i can make a public variable and that's it...

sorry it takes me so long to understand that...
thanks for the patient. :)

There are some great explanations in this thread.

When you use the short version

public int MyVar { get; set; }

The C# compiler is smart enough to work this syntax out to the full version. With a sectretly generated field variable.
You use it for simple properties like color etc. You just have to set or get a color. Suppose you have to make 100 color properties. Saves you a lot of typing! Let the compiler do this work for you!
On the other hand if you have a property like LoanAmount you have to use the normal,long version, because you probably want to add some taxes, do a check if the right person is logged in or whatever.
Hope this clears things up. ;)

Basically a property is a method with the syntax of a member. From a developers perspective:

public int MyVar { get; set; }

Has the exact same functionality as

public int MyVar;

It's actually slightly more efficient to use the second example, since the compiler doesn't need to implement the get and set methods internally.

One thing that makes these automatic properties a bit more useful is the ability to independently set the access type of gets and sets:

public MyVar {get; private set;}

This means that only within your class may this property be set, but the value may be read as a read-only value outside the scope of your class.

When you use the short version
C# Syntax (Toggle Plain Text)
public int MyVar { get; set; } public int MyVar { get; set; }
The C# compiler is smart enough to work this syntax out to the full version. With a sectretly generated field variable.
You use it for simple properties like color etc. You just have to set or get a color. Suppose you have to make 100 color properties. Saves you a lot of typing! Let the compiler do this work for you!
On the other hand if you have a property like LoanAmount you have to use the normal,long version, because you probably want to add some taxes, do a check if the right person is logged in or whatever.
Hope this clears things up.

Basically a property is a method with the syntax of a member. From a developers perspective:


C# Syntax (Toggle Plain Text)
public int MyVar { get; set; }public int MyVar { get; set; }

Has the exact same functionality as

C# Syntax (Toggle Plain Text)
public int MyVar;public int MyVar;

It's actually slightly more efficient to use the second example, since the compiler doesn't need to implement the get and set methods internally.

One thing that makes these automatic properties a bit more useful is the ability to independently set the access type of gets and sets:


C# Syntax (Toggle Plain Text)
public MyVar {get; private set;}public MyVar {get; private set;}

This means that only within your class may this property be set, but the value may be read as a read-only value outside the scope of your class.

after the two last answers, i am little confused...
1. when i need to change things so i need to use full version with private & public
2. when i need only to change accesibiliity its ok to use the shortcut because the is possibility to add private to the get or set
3. when i do not need to do both its better to simply use the "public int MyVar;"

so - why i need the "public int MyVar {get; set;}"? i can use the 3'rd way.
and i understand that the shortcut is only for the 2'nd thing when i can put private before the get or set.

am i right? there is no need to put "public int MyVar {get; set;}" in my program?

thanks... :)

You are absolutely right. There is no need for a property if you want the member to be fully public and able to accept any value.

BUT make sure you think carefully about which members/methods/properties you want to make completely public. Not only can they fill up intellisence with garbage you will never use, it will also have potentially bad side effects when people modify things your class that they shouldn't have access to, whether it be you or someone you are working on the code with.

You are absolutely right. There is no need for a property if you want the member to be fully public and able to accept any value.

BUT make sure you think carefully about which members/methods/properties you want to make completely public. Not only can they fill up intellisence with garbage you will never use, it will also have potentially bad side effects when people modify things your class that they shouldn't have access to, whether it be you or someone you are working on the code with.

I understand that... i saw a lot of tutorials that automatically put get/set on every field, and i started to do that automatically even if i didnt need to handle things on the field or do the get/set private.

after i did that on all my fields (every field had a private & public get/set) i found that only 2 or 3 times in a very big code i need to handle the get/set.

because of that i am very confused, its like i need to change my style that i thought its need to be like and move to - use get/set only when its needed...

its make sense - its always make sense - but i have a feeling that i miss something... am i not?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.