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

Lecture 04 - programming fundamentals

Uploaded by

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

Lecture 04 - programming fundamentals

Uploaded by

anjalee himalki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Language Fundamentals III

Object oriented with visual programming


M. Y. A Rahman
Program 09: write a set and get function for a new baby’s name and DOB who is born today

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

• namespace consoleprogram8
• {
• internal class Program
• {
• public class CreateInstance
• {
• public String name { get; set; } = "New born"; //set default to auto properties
• public DateTime dob { get; set; } = DateTime.Now;
• }

• static void Main(string[] args)
• {
• var obj = new CreateInstance();
• Console.WriteLine("Name is: "+obj.name);
• Console.WriteLine("DOB is: "+obj.dob);
• Console.ReadKey();


• }
• }
• }
Method in C#

• A method is a code block that contains a series of statements.


• A program causes the statements to be executed by calling the method and
specifying any required method arguments.
• In C# every executed instruction is performed in the context of a method.
• The Main method is the entry point for every C# application and it it called
by the common language runtime (CLR) when the program is started
Method in C# cont.

• A method is a group of statements that together performed a task. Every


C# program has at least one class with a method named Main.
• To use a method, you need to :
• Define a method
• Call the method
Defining method in C#

• When you define a method, you basically declare the element of its
structure. The syntax for defining a method in c# is as follows:
• <Access Specifier> <Return Type> <method Name>(Parameter List)
{
Method body
}
Defining method in C# cont.,
• Following are the various element of a method:
• Access Specifier: this determines the visibility of a variable or a method from another
class.
• Return type: A method may return a value. The return type is the data type of the value
the method returns. If the method is not returning any values, then the return type is
void.
• Method name: Method name is a unique identifier and it is case sensitive. it cannot be
same as any other identifier declared in the class.
• Parameter list: Enclosed between paratheses, the parameters are used to pass and
receive data from a method. The parameter list refers to the type, order, and number
of the parameters of a method. Parameters are optional; that is, a method may contain
no parameter.
• Method body: This contains the set of instructions needed to complete the required
activity.
Example
• Void return nothing
• Public void name
• {
• Console.WriteLine(“Welcome to C#”);
• }
• Return a string value
• Public string value
• {
• Return “hi”;
• }
• Method with two parameter and return integer
• Public int calculate(int a, int b)
• {
• Return a+b;
• }
Sample program
• Create a class to find the area of a rectangle.
• Area=length* width
• Define a class to find the answer, define suitable data members and
methods of the class:

Enter values for length: 4.5


Enter value for width:8.3
------
Length: 4.5
Width: 8.3
Area: 37.35
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;

• namespace consoleprogram9
• {
• public class Rectangle
• {
• public double length;
• public double width;
• public double area;
• public void setdata() //creating a method which return nothing
• {
• Console.Write(" Enter value for length:");
• length= Convert.ToDouble(Console.ReadLine());
• Console.Write(" Enter value for width:");
• width = Convert.ToDouble(Console.ReadLine());
• }
• public void calarea()// creating a method which return nothing
• {
• area = length * width;
• }

• public void displayData()


• {
• Console.WriteLine("Length = {0}", length);
• Console.WriteLine("Width = {0}", width);
• Console.WriteLine("Area = {0}", area);
• }
• }
• internal class Program
• {
• static void Main(string[] args)
• {
• Rectangle rectangle = new Rectangle();
• rectangle.setdata();//Accesssing the method through the
rectangle class
• rectangle.calarea();
• rectangle.displayData();
• Console.ReadKey();
• }
• }
• }
Static class
• Static class is declared using the “static” keyword.
• If the class is static then the compiler never creates an instance of the class.
• All the members field, properties and functions must be declared as static
• They are accessed by the class name directly not by a class instance object.
Static class cont.,
• Creating a static class is therefore basically the same as creating a class that
contains only static members and a private constructor. A private
constructor prevents the class from being instantiated.
• The advantage of using a static class is that the compiler can check to make
sure that no instances members are accidently added. The compiler will
guarantee that instances of this class can not be created.
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;

• namespace consoleprogram10
• {
• internal class Program
• {
• class staticclass
• {
• public static int x { get; set; } = 10;
• public static void calculate()
• {
• x = 2 * 2;
• Console.WriteLine($"Answer is:{x}");
• Console.ReadKey();
• }
• }
• public class staticExecute
• {
• static void Main(string[] args)
• {
• var a = new staticclass();
• staticclass.calculate();
• }
• }
• }
• }
Find out in google what is the practical use of
the static class?
• Practical use of static class is as follows:
• Static classes are used as containers for static members.
• The most often utilized components of a static class are static methods and static
attributes.
• The class name is used to immediately access all static members.
• Instead to employing the instance of a type, static methods are called directly using a
type name and perform a specific task.
Constructor

• When a class created, its constructor is called.


• Specialized function – used to initialize fields.
• Has the same name as the class.
• Constructor returns void but does not have an explicitly declared return
type.
Constructors.,

• Constructor type:
• Default constructors
• Parameterized constructors
• Private constructors
• Copy contractors
Constructor example

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

• namespace consoleprogram11
• {
• public class constrcutors
• {
• public string name { get; set; } = "Silva";
• public int age { get; set; } = 20;
• public static string address { get; set; } = "Colombo";
• public constrcutors() //Default Constructor Declaration
• {
• name = "Perera";
• age = 34;
• }
• public constrcutors(string x, int y) //parametarized Constructor Declaration
• {
• name = x;
• age = y;
• }
• private constrcutors(string x,string y,int z) //private Constructor
Declaration
• {
• name = x;
• address = y;
• age = z;
• }
• public constrcutors(constrcutors c) //copy Constructor Declaration
• {
• name = c.name;
• age = c.age;
• }
• public void display() => Console.WriteLine($"Name:{name} \n
Age:{age}");
• public static void
displayPrivate()=>Console.WriteLine($"Address: {address}");

• }
• internal class Program
• {
• static void Main(string[] args)
• {
• Console.WriteLine("Default Constructor Result");
• var b = new constrcutors(); //invoking default constructors
• b.display();

• Console.WriteLine("\n Parameter result");


• var c = new constrcutors("Wickrama", 45);
• c.display();

• Console.WriteLine("\n private parameter");


• constrcutors.displayPrivate();

• Console.WriteLine("\n Copy constrcutor");


• var d = new constrcutors(c);
• d.display();

• Console.ReadKey();

• }
• }
• }
• /* Cannot create objects for a private constructor. Therefore methods of the
class can only access with the class name. To access by the class name;
methods can access only static variables. Therefore it can access only
address property */
• The => token is supported in two forms:
• as the lambda operator and,
• as a separator of a member name and the member implementation in an expression
body definition.
• Note:
• Constructors are mostly used to initialize a class's private fields when a class instance is
created. If you don't add a constructor to a class, the compiler will do it for you and
produce a default constructor.
• Constructors are used to initialize an object to its default or initial state. You might not
find what you're seeking for in the primitives' default values. Utilizing constructors is
also advantageous because they inform about dependency.
Destructor
• Purpose – is to remove unused objects and resources
• Destructors are not called directly in the source code but during garbage
collection.
• It is invoked at an undetermined moment.
• Programmer cant control its execution; rathe it is called by the finalized ()
method.
• Has the same name as the class with a tilde(~).
• Destructors are parameters less.
• Destructors are not inherited
• ~construction()
{
Console.writeline(“\Destructor works”);
}
Thank you

You might also like