C# Object and Classes

//having main in another class
using System;

namespace OObj1
{
    class Student1
    {
        public int id;
        public string name;
        
    }

    class TestStudent
    {
        public static void Main(String[] args)
        {
            Student1 s1=new Student1();
            s1.id = 200;
            s1.name = "Dhaa";
            Console.WriteLine(s1.id);
            Console.WriteLine(s1.name);
            Console.ReadKey();
        }
    }


}
//having main in same class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OObj1
{
    class Student
    {
        int id;
        String name;
        static void Main(string[] args)
        {
            Student s1=new Student();// creating an object of student
            s1.id = 101;
            s1.name = "Zaki";
            Console.WriteLine(s1.id);
            Console.WriteLine(s1.name);
            Console.ReadKey();
        }
    }
}
//initialize and display data through method
using System;

namespace OOMethod
{
    class Student
    {
        public int id;
        public String name;

        public void insertOOP(int i, String n)
        {
            id = i;
            name = n;
        }

        public void display()
        {
            Console.WriteLine(id + " " + name);
        }

    }

    class Printing { 
    static void Main(string[] args)
        {
            Student s1=new Student();
            Student s2=new Student();
            s1.insertOOP(101,"Zaki");
            s2.insertOOP(209,"Live");
            s1.display();
            s2.display();
            Console.ReadKey();
        }
    }
}
//store and display employee information using method
using System;

namespace OOMethod
{
    class Employee
    {
        public int id;
        public string name;
        public float salary;

        public void insert(int i,string n,float s)
        {
            id = i;
            name = n;
            salary = s;
        }

        public void display()
        {
            Console.WriteLine(id+" "+name+" "+salary);
        }
    }

    class TestEmployee
    {
        public static void Main(String[] args)
        {
            Employee e1=new Employee();
            Employee e2=new Employee();
            e1.insert(101,"Zaki",9898f);
            e2.insert(202,"Live",999f);
            e1.display();
            e2.display();
            Console.ReadKey();
        }
    }
}
//"this" keyword it is used if field names(instance variable) and parameters names are same in a method, that is why both can be distinguish easily

using System;

namespace UsingThis
{

    public class Employee
    {
        public int id;
        public string name;
        public float salary;


        public Employee(int id,string name, float salary)
        {
            this.id = id;
            this.name = name;
            this.salary = salary;
        }

        public void Display()
        { 
            Console.WriteLine(id+" "+name+" "+salary);
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Employee e1=new Employee(101,"Zaki",101.55f);
            Employee e2=new Employee(2015,"Live",665.55f);
            e1.Display();
            e2.Display();
            Console.ReadKey();
        }
    }
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.