Instance, class and Local variable Demo in C#.NET
Instance Variables Demo Program
/*
Title :
Instance Variables Demo
CreatedBy : Ramana Reddy
CreatedDate : 2023-July-07
*/
using System;
namespace InstanceVariableDemo
{
class StudentMarks
{
// instance variables
string sname;
int
telMarks;
int
engMarks;
int
hndMarks;
int
socMarks;
int sciMarks;
static void Main(string[] args)
{
StudentMarks instance1 = new StudentMarks();
instance1.sname = "Ramana";
instance1.telMarks = 75;
instance1.engMarks = 80;
instance1.hndMarks = 76;
instance1.sciMarks = 65;
instance1.socMarks = 70;
StudentMarks instance2 = new StudentMarks();
instance2.sname = "Harshit";
instance2.telMarks = 85;
instance2.engMarks = 75;
instance2.hndMarks = 65;
instance2.sciMarks = 85;
instance2.socMarks = 80;
// Printing student mark detials
Console.WriteLine($"\n Student {instance1.sname} mark detail's:");
Console.WriteLine(" ---------------------------\n");
Console.WriteLine(" English subject marks: " + instance1.engMarks);
Console.WriteLine(" Telugu subject marks : " + instance1.telMarks);
Console.WriteLine(" Hindi subject marks : " + instance1.hndMarks);
Console.WriteLine(" Science subject marks: " + instance1.sciMarks);
Console.WriteLine(" Social subject marks : " + instance1.socMarks);
Console.WriteLine("_______________________________________");
Console.WriteLine($"\n Student {instance2.sname} mark detail's:");
Console.WriteLine(" ----------------------------\n");
Console.WriteLine(" English subject marks: " + instance2.engMarks);
Console.WriteLine(" Telugu subject marks : " + instance2.telMarks);
Console.WriteLine(" Hindi subject marks : " + instance2.hndMarks);
Console.WriteLine(" Science subject marks: " + instance2.sciMarks);
Console.WriteLine(" Social subject marks : " + instance2.socMarks);
Console.ReadLine();
}
}
}
Class level variables Demo
/*
Title :
Instance & Class Variables Demo
CreatedBy : Ramana Reddy
CreatedDate : 2023-July-07
Desc : Class level variables also known as
static variables and these can be
accessed using classname directly
*/
using System;
namespace InstanceClassVariableDemo
{
class StudentMarks
{
// instance variables
string sname;
int
telMarks;
int
engMarks;
int
hndMarks;
int
socMarks;
int sciMarks;
//class level variables
static string clgName = "HSR Engg.College";
static double clgFee = 55000d;
static void Main(string[] args)
{
StudentMarks instance1 = new StudentMarks();
instance1.sname = "Ramana";
instance1.telMarks = 75;
instance1.engMarks = 80;
instance1.hndMarks = 76;
instance1.sciMarks = 65;
instance1.socMarks = 70;
StudentMarks instance2 = new StudentMarks();
instance2.sname = "Harshit";
instance2.telMarks = 85;
instance2.engMarks = 75;
instance2.hndMarks = 65;
instance2.sciMarks = 85;
instance2.socMarks = 80;
// Printing College,Fee details with class level variable
Console.WriteLine("________________________________________");
Console.WriteLine("\n College Name : " + StudentMarks.clgName);
Console.WriteLine("\n College Fee : " + StudentMarks.clgFee);
Console.WriteLine("________________________________________\n");
// Printing student mark detials
Console.WriteLine($"\n Student {instance1.sname} mark detail's:");
Console.WriteLine(" ---------------------------\n");
Console.WriteLine(" English subject marks: " + instance1.engMarks);
Console.WriteLine(" Telugu subject marks : " + instance1.telMarks);
Console.WriteLine(" Hindi subject marks : " + instance1.hndMarks);
Console.WriteLine(" Science subject marks: " + instance1.sciMarks);
Console.WriteLine(" Social subject marks : " + instance1.socMarks);
Console.WriteLine("_______________________________________");
Console.WriteLine($"\n Student {instance2.sname} mark detail's:");
Console.WriteLine(" ----------------------------\n");
Console.WriteLine(" English subject marks: " + instance2.engMarks);
Console.WriteLine(" Telugu subject marks : " + instance2.telMarks);
Console.WriteLine(" Hindi subject marks : " + instance2.hndMarks);
Console.WriteLine(" Science subject marks: " + instance2.sciMarks);
Console.WriteLine(" Social subject marks : " + instance2.socMarks);
Console.ReadLine();
}
}
}
Local variables Demo
/*
Title :
Local & Parameter variables Demo
CreatedBy : Ramana Reddy
CreatedDate : 2023-July-07
*/
using System;
namespace LPVaraiblesDemo
{
class Calculator
{
int Add(int value1, int value2)
{
return (value1 + value2);
}
static void Main(string[] args)
{
// Local variables
int a, b, sum;
Console.WriteLine("Enter a value :");
a = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter b value :");
b = Convert.ToInt16(Console.ReadLine());
// instance creation to calculator calss
Calculator objCal = new Calculator();
// calling Add function
sum = objCal.Add(a, b);
Console.WriteLine("__________________________\n");
Console.WriteLine($"Entered values a :{a} and b:{b}");
Console.WriteLine("\nSum of entered values is :" + sum);
Console.ReadLine();
}
}
}
Comments
Post a Comment