Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

/*************************************************************************

* Properties in C# Example
*
*************************************************************************/
using System;
namespace MyProject.Examples
{
public class Book
{
private int _id;
private string _bookName;
private int _noOfPAges=250;
private string _auther;

public string Auther{get; set;}

public int Id
{
set
{
if (value < 0)
throw new Exception("Id not valid");
this._id = value;
}
get {
return this._id;
}
}

public string BookName


{
set
{
if (string.IsNullOrEmpty(value))
throw new Exception("Book Name not valid or null");
this._bookName = value;
}
get {
return this._bookName;
}
}

public int NoOfPAge


{
get
{
return this._noOfPAges;
}
}
}

class ExampleOne
{
public static void Main()
{

Book B1 =new Book();


B1.Id=10;
B1.BookName = "The C# Book";
B1.Auther = "ProgrammingKnowldge";
Console.WriteLine("The Book Id is {0}", B1.Id);
Console.WriteLine("The Book Name is {0}", B1.BookName);
Console.WriteLine("The Book Page is {0}", B1.NoOfPAge);
Console.WriteLine("The Book Auther is {0}", B1.Auther);
Console.ReadKey();
}
}
}

You might also like