TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Use Get and Set in C# console Programming
Rajan Singh
Jun 28
2015
Code
1.4
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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