0% found this document useful (0 votes)
20 views8 pages

Class

There are two main types of datatypes in C#: 1) Built-in datatypes like int, char, float, double 2) User-defined datatypes which can be structures, classes, arrays, etc. Structures are value types while classes are reference types. Arrays can hold multiple objects of a class. The 'this' keyword refers to the current instance of a class and is used to distinguish between class fields and method parameters when they have the same name.

Uploaded by

arrchu1687
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Class

There are two main types of datatypes in C#: 1) Built-in datatypes like int, char, float, double 2) User-defined datatypes which can be structures, classes, arrays, etc. Structures are value types while classes are reference types. Arrays can hold multiple objects of a class. The 'this' keyword refers to the current instance of a class and is used to distinguish between class fields and method parameters when they have the same name.

Uploaded by

arrchu1687
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

there are 2 types of Datatype

1)Built in
int ,char,float,double
int a;
2)Userdefine datatype

A)structure/union
C# Structs
In C#, classes and structs are blueprints that are used to create instance of a
class.
Structs are used for lightweight objects such as Color, Rectangle, Point etc.

Unlike class, structs in C# are value type than reference type. It is useful if you
have data that is not intended to be modified after creation of struct.

struct name
{
datatypr var1;
datatype var2;
.
.
..
};
struct stud
{
int rollno;
char name[20];
float per;
};
struct stud s1;
(.)member of
s1.rollno=123;
s1.name="abc"
s1.per=12.67

C# Struct Example
Let's see a simple example of struct Rectangle which has two data members width and
height.

using System;
public struct Rectangle
{
public int width, height;

}
public class TestStructs
{
public static void Main()
{

Rectangle r = new Rectangle();


r.width = 4;
r.height = 5;
Console.WriteLine("Area of Rectangle is: " + (r.width * r.height));
}
}

class
userdefine datatype
collection of member datatype & methods
blueprint of an object
collection of an object

using System;
class Student
{
int rno;
string sname;
double per;
public void accept()
{

Console.WriteLine("Enter rollno ,name & per");


rno=Convert.ToInt32(Console.ReadLine());
sname=Console.ReadLine();
per=Convert.ToDouble(Console.ReadLine());
}
public void display()
{
Console.WriteLine("Rollno={0}",rno);
Console.WriteLine("Name={0}",sname);
Console.WriteLine("Per={0}",per);

}
}

public class Stud_Demo {

public static void Main(string[] args) {


Student s1=new Student();

s1.accept();
s1.display();
Student s2=new Student();
s2.accept();
s2.display();
Student s3=new Student();
s3.accept();
s3.display();

2)Book
bid
bname,author
price

3)Vehicle
int vid
string vanme,compname,color,ownern
double price

4)
Employee
eid,ename,designation,email,addr,salary

5)
using System;
class Max
{
int a,b;

public void Accept()


{

Console.WriteLine("Enter 2 number");
a=Convert.ToInt32(Console.ReadLine());
b=Convert.ToInt32(Console.ReadLine());

}
public void CalMax()
{ if(a>b)
Console.WriteLine("MAx={0}",a);
else
Console.WriteLine("MAx={0}",b);

}
}

public class Max_Demo


{

public static void Main(string[] args)


{
Max m1=new Max();
m1.Accept();
m1.CalMax();

6)Perform any 5 programs(MAgic,Prime,Factorial,pattern,Disarium)4)

using System;
public class Area_Demo
{
double r,A;
public void accept(double r)//double r1
{
this.r=r;//r=r1
}
public double cal_area()
{
A=3.14*r*r;
return(A);
}

}
class Area_Ex
{
public static void Main(string[] args)
{
double r1,A;

Area_Demo a1=new Area_Demo();


Console.WriteLine("Enter value of r");
r1=Convert.ToDouble(Console.ReadLine());
a1.accept(r1);
A=a1.cal_area();
Console.WriteLine("Area={0}",A);
}

}
public class Fact_Demo
{
int n,f1=1,i;
void accept(int n)
{
this.n=n;
}
int cal_fact()
{
for()
{
}
return(f1);

}
}
public class Main
{
public static void main(String[] args)
{
int n,f1;

Fact_Demo a1=new Fact_Demo();


CW("Enter value of r");
n=CR;
a1.accept(n);
f1=a1.cal_fact();
CR("Fact="+f1);
}

}
5)max from 2 number
6)factorial
6.1)REVERSE,ARMSTRONG,MAGIC,MAX FROM 3

7)
using System;
class ParaMethodDemo
{
int flag=0,n1,n,x,sum=0,p,f1=1,i;
public void accept(int n)//(int n1)
{
this.n=n; //n=n1;
}
public void pattern()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
Console.Write("{0} ",j);
}
Console.WriteLine();
}
}
public void prime()
{

for(i=2;i<=(n/2);i++)
{
if(n%i==0)
{
flag=1;
break;
}
}

if(flag==0)
Console.WriteLine("No is prime");
else
Console.WriteLine("No is not prime");
}
public string pal()
{
p=n;
while(p>0)
{
n1=p%10;
p=p/10;
sum=(sum*10)+n1;
}

if(sum==n)
return "No is pal";
else
return "No is not pal";
}

public int power(int x)


{
f1=1;

this.x=x;//this means acces the member of itself


for(i=1;i<=n;i++)
{
f1=f1*x;
}
return (f1);

}
}
class Demo
{
public static void Main(string[] args)
{
int n,x;

ParaMethodDemo a1=new ParaMethodDemo();


Console.WriteLine("Enter value of n");
n=Convert.ToInt32(Console.ReadLine());
a1.accept(n);
a1.pattern();

a1.prime();
Console.WriteLine("{0}",a1.pal());
Console.WriteLine("Enter value of x");
x=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Power={0}",a1.power(x));

8)Any 4 favourite functions add in class use any three types of userdefine function
9)Any 4 favourite functions add in class use any three types of userdefine function

* Array of an Object

using System;
class Student
{
int rno;
string sname;
double per;
public void accept()
{

Console.WriteLine("Enter rollno ,name & per");


rno=Convert.ToInt32(Console.ReadLine());
sname=Console.ReadLine();
per=Convert.ToDouble(Console.ReadLine());
}
public void display()
{
Console.WriteLine("Rollno={0}",rno);
Console.WriteLine("Name={0}",sname);
Console.WriteLine("Per={0}",per);

}
}

public class Stud_Demo {

public static void Main(string[] args) {


int n,i;
Console.WriteLine("Enter no of students");
n=Convert.ToInt32(Console.ReadLine());
Student []s1=new Student[n];//Array creation
for(i=0;i<n;i++)
{
s1[i]=new Student();//object creation
s1[i].accept();
s1[i].display();
}

2)
using System;
public class Area_Demo
{
double r,A;
public void accept(double r1)
{
r=r1;
}
public double cal_area()
{
A=3.14*r*r;
return(A);
}

}
class Area_Ex
{
public static void Main(string[] args)
{
double r;
int n,i;

Console.WriteLine("Enter no of records");
n=Convert.ToInt32(Console.ReadLine());

Area_Demo []a1=new Area_Demo[n];//array creation


for(i=0;i<n;i++)
{
a1[i]=new Area_Demo();//object creation
Console.WriteLine("Enter value of r");
r=Convert.ToDouble(Console.ReadLine());
a1[i].accept(r);
Console.WriteLine("Area={0}",a1[i].cal_area());
}

Task
Convert all above programs into Array of an object type

C# this
In c# programming, this is a keyword that refers to the current instance of the
class. There can be 3 main usage of this keyword in C#.

It can be used to refer current class instance variable. It is used if field names
(instance variables) and parameter names are same, that is why both can be
distinguish easily.
It can be used to pass current object as a parameter to another method.
It can be used to declare indexers.
C# this example
Let's see the example of this keyword in C# that refers to the fields of current
class.

You might also like