Click here to Skip to main content
16,018,418 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi ,



What is the difference between

1)


Private int myInt;

public int MyInt
{
get { return _myInt.Value; }
set { _myInt.Value = value; }
}

2)

public int MyInt;


what is the use of this

public int MyInt
{
get { return _myInt.Value; }
set { _myInt.Value = value; }
}

could any one help me with a example.
Posted
Comments
Sergey Alexandrovich Kryukov 23-Apr-14 10:04am    
It is unrelated to ASP.NET. Why not reading standard MSDN documentation?
Anyway, you did not show any correct code.
—SA

First of all, none of your code sample will compile. Let's try to reformulate the question the way it compiles:
VB
class SomeDeclaringType {
    
    private int myInt; // "private" could be omitted; this is the default
    // what happens if you change it to "public"?

    public int MyInt { // could be "internal" instead of "private"
        get { return _myInt.Value; }
        set { _myInt.Value = value; }
    }
    
    //...

}

First of all, you could easily find all answers here: http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx[
title="New Window">^
].
Read about the use of accessors; I don't want to repeat the documentation.

You could compare the code with the case where you change private to public (or internal; by the way, read about access modifiers: http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx[^].) What's the difference? Well, it would work the same way, only the code would be totally pointless: you would expose myInt to the user which would provide exact same functionality as MyInt. Isn't it obvious?

Moreover, you don't need to have myInt at all, because those trivial getter and setter can be auto-implemented: http://msdn.microsoft.com/en-us/library/bb384054.aspx[^].

So, this code would be 100% identical to the first code sample:
C#
class SomeDeclaringType {
    public int MyInt { get; set; }
    //...
}

Note that this property provides same exact functionality as just the public field. Why we use those trivial properties then? Well, this is mostly the element of culture, but also the maintainability of the code. First of all, using any public (or even internal) fields is considered bad style; and using properties is considered good style. Again, why? Because the public/internal member is a part of contract. And the property allows to add functionality without changing contract. You can do it by adding a body to the setter and getter; and this body is only needed when you need to add a side effect to the operation of reading of the property value or assigning the value to the property.

—SA
 
Share this answer
 
v2
Comments
Ajith K Gatty 23-Apr-14 12:30pm    
Hi sir,
I removed the solution.It was not proper. Thank you for informing.
Sergey Alexandrovich Kryukov 23-Apr-14 15:32pm    
I appreciate your understanding, thank you.
—SA
Well, ther share a common factor: neither of them will compile...

Really?
1) int does not have a Value property. So either your property setters and getters are wrong, or _myInt is badly named...
2) The second version tries to declare two different objects with the same name: MyInt cannot be a variable and a property in the same scope.

I think you need to go back to your homework question, and look a little closer at exactly what it is asking you: because it isn't that!
 
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