Lecture 04 - programming fundamentals
Lecture 04 - programming fundamentals
• 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#
• 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:
• 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;
• }
• 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
• 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.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