Lecture 03 - programming fundamentals
Lecture 03 - programming fundamentals
Public Public class is visible in the current and referencing assembly. (Access is not
restricted)
Private Visible inside current class
Internal protected Visible inside containing assembly and descending of the current class
• Modifiers define the declaration of a class
• The Base list is the inherited class
• By default, classes inherit from the system.object type.
• A class can inherit and implement multiple interfaces but doesn't support
multiple inheritances.
Creating class & Instance
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;
• namespace consoleprogram6
• {
• internal class Program
• {
• //Creating a class
• class Customer
• {
• public int cusid = 100;
• public String name = "Gayan";
• }
• static void Main(string[] args)
• {
• //creating a class object
• Customer obj = new Customer();
• Console.WriteLine(obj.name);
• Console.WriteLine(obj.cusid);
• Console.ReadKey();
• }
• }
• }
GET and SET
• Getters and setters are used to protect your data, particularly when creating
classes.
• The get method returns the value of the variable name . The set method
assigns a value to the name variable
• For each instance variable, a getter method returns its value while a setter
method sets or updates its value. Given this, getters and setters are also
known as accessors and mutators, respectively.
GET and SET cont.,
• The code block for the get accessors is executed when the property is read;
the code block for the set accessor is executed when the property is
assigned a new value.
• A property without a set accessor is considered read-only. A property
without a get accessor is considered write only. A property that has both
accessors is read-write
• Property is called as a smart variable that make private variables as
properties and assign values.
Program 07: write a console program with get and set(name and DOB)
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;
• namespace consoleprogram7
• {
• class student
• {
• String name;
• String dob;
• public String Name
• {
• get { return name; }
• set { name = value; }
• }
• public String Dob
• {
• get { return dob; }
• set { dob = value; }
• }
• }
• internal class Program
• {
• static void Main(string[] args)
• {
• student student = new student();
• student.Name = "Kasun";
• student.Dob = "1999-11-10";
• Console.WriteLine("Name is: "+student.Name + "DOB is: "+student.Dob);
• Console.ReadKey();
• }
• }
• }