C# property get

You will use the get keyword to define read-only properties of a class. You can use the read-only properties to read data from private data members of the class, but you can not use them to write data to the members. In the example below, data are written to private data members (id,title, ISBN,author,year) of the class Book through the constructor when the object of the Book class is created and data are read by using properties.

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

namespace Csharpproj
{
   class Book
   {
     private string id;
     private string title;
     private string isbn;
     private string author;
     private int year;
     public Book(string ID, string Title, string ISBN, string Author, int Year)
     {
        id = ID;
        title = Title;
        isbn = ISBN;
        author = Author;
        year = Year;

     }
     public string ID
     {
        get { return id; } //read only property ID to read data from id data member
     }
     public string Title
     {
        get { return title; } //read only property Title to read data from title member
     }
     public string ISBN
     {
        get { return isbn; } //read only property ISBN to read data from isbn
     }
     public string Author
     {
        get { return author; } //read only property Author to read data from author
     }
     public int Year
     {
        get { return year; } //read only property Year to read data from year
     }



   }
   class Program
   {
     static void Main(string[] args)
     {
        Book bk = new Book("C1013", "C# programming for beginners", "1-10-90987-4", "Yuk Sovandara",2013);
        Console.WriteLine("ID:{0}\nTilte:{1}\nISBN{2}\nAuthor:{3}\nYear:{4}", bk.ID, bk.Title, bk.ISBN, bk.Author, bk.Year);
      
        Console.Read();
     }
   }
}

Posted by: Dara | post date: 02-08-2013 | Subject: C# Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.