0% found this document useful (0 votes)
4 views

18# Encapsulation Object_Oriented Programming Getter & Setter

The document contains multiple versions of a C# class named 'User' that demonstrates encapsulation in object-oriented programming using getters and setters. It includes properties for userID, userName, firstName, and lastName, with variations that enforce validation on userID and provide a full name property. The 'Program' class creates instances of 'User' and displays user information.

Uploaded by

Lesunter KLter
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

18# Encapsulation Object_Oriented Programming Getter & Setter

The document contains multiple versions of a C# class named 'User' that demonstrates encapsulation in object-oriented programming using getters and setters. It includes properties for userID, userName, firstName, and lastName, with variations that enforce validation on userID and provide a full name property. The 'Program' class creates instances of 'User' and displays user information.

Uploaded by

Lesunter KLter
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

18# Encapsulation Object_Oriented Programming Getter & Setter

////////////
//User.cs//
//\\\\\\\\\

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace darklter
{
internal class User
{
private int userID;
private string userName;
private string firstName, lastName;

public int UserID


{
get { return userID; }
set { userID = value; }
}

public string UserName


{
get { return userName; }
set { userName = value; }
}

public string FirstName


{
get { return firstName; }
set { firstName = value; }
}

public string LastName


{
get { return lastName; }
set { lastName = value; }
}
}
}

///////////////
//Program.cs//
//\\\\\\\\\\\\

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
User user = new User();
user.UserID = 1001;

Console.WriteLine(user.UserID);
}
}
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||||

////////////
//User.cs//
//\\\\\\\\\

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace darklter
{
internal class User
{
private int userID;
private string userName;
private string firstName, lastName;

public int UserID


{
get { return userID; }
set
{
if (value >= 1000) userID = value;
else Console.WriteLine("Error User ID must not be below 1000");
}
}

public string UserName


{
get { return userName; }
set { userName = value; }
}

public string FirstName


{
get { return firstName; }
set { firstName = value; }
}

public string LastName


{
get { return lastName; }
set { lastName = value; }
}

public User(int userID, string userName, string firstName, string lastName)


{
UserID = userID;
UserName = userName;
FirstName = firstName;
LastName = lastName;
}
}
}

///////////////
//Program.cs//
//\\\\\\\\\\\\

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
User user = new User(999,"sdptDavid","David","Sdpt");
}
}
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||

////////////
//User.cs//
//\\\\\\\\\

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace darklter
{
internal class User
{
private int userID;
private string userName;
private string firstName, lastName;

public int UserID


{
get { return userID; }
set { userID = value; }
}

public string UserName


{
get { return userName; }
set { userName = value; }
}

public string FirstName


{
get { return firstName; }
set { firstName = value; }
}

public string LastName


{
get { return lastName; }
set { lastName = value; }
}

public string FullName


{
get { return firstName + " " + lastName; }
}

public User(int userID, string userName, string firstName, string lastName)


{
UserID = userID;
UserName = userName;
FirstName = firstName;
LastName = lastName;
}
}
}

///////////////
//Program.cs//
//\\\\\\\\\\\\

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
User user = new User(1000,"sdptDavid","David","Sdpt");

Console.WriteLine(user.FullName);
}
}
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||

////////////
//User.cs//
//\\\\\\\\\

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace darklter
{
internal class User
{
public int userID { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }

public User(int userID, string userName, string firstName, string lastName)


{
this.userID = userID;
this.userName = userName;
this.firstName = firstName;
this.lastName = lastName;
}
}
}

///////////////
//Program.cs//
//\\\\\\\\\\\\

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
User user = new User(1000, "sdptDavid", "David", "Sdpt");
}
}
}

You might also like