Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Use Get and Set in C# console Programming
WhatsApp
Rajan Singh
Jun 28
2015
1.5
k
0
0
using
System.IO;
using
System;
class
PropertyClass
{
private
int
TheRealValue;
// Field: memory allocated
public
int
MyValue
// Property: no memory allocated
{
set
{
TheRealValue = value;
}
get
{
return
TheRealValue;
}
}
class
AccessingPropertyExternalClass
{
public
static
void
Main(String[] arg)
{
PropertyClass propertyObject =
new
PropertyClass();
int
fieldValue;
fieldValue = propertyObject.MyValue;
//Get Field Value via Property's set Accessor
Console.WriteLine(
"MyValue: "
+ fieldValue);
propertyObject.MyValue = 100;
//Set Field Value via Property's set Accessor
fieldValue = propertyObject.MyValue;
Console.WriteLine(
"MyValue: "
+ propertyObject.MyValue);
}
}
}
Output
MyValue: 0
MyValue: 100
C#
GET
SET
Up Next
Use Get and Set in C# console Programming