Simple console application using Get and Set Methods
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplicationSample
{
class GetSetMethod
{
private string myName = "GetSetMethod";
private int myAge = 22;
//
Declare a Name property of type string:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
//
Declare an Age property of type int:
public int Age
{
get
{
return myAge;
}
set
{
myAge = value;
}
}
public override string ToString()
{
return "Name = " + Name + ", Age =
" + Age;
}
public static void Main()
{
Console.WriteLine("Simple
Properties");
//
Create a new Person object:
GetSetMethod person = new GetSetMethod();
// Print
out the name and the age associated with the person:
Console.WriteLine("Person
details - {0}", person);
// Set
some values on the person object:
person.Name = "Joe";
person.Age = 99;
Console.WriteLine("Person
details - {0}", person);
//
Increment the Age property:
person.Age += 1;
Console.WriteLine("Person
details - {0}", person);
Console.ReadKey();
}
}
}
https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos
0 comments
Post a Comment