PROGRAM-1
Print a basic code:-----
using System;
class Program{
static void Main(string[] args){
[Link]("Welcome to BCA Third Year");
}
}
OUTPUT
Welcome to BCA Third Year
PROGRAM-2
using System;
class Program
{
static void Main()
{
[Link]("Enter your age: ");
int age = Convert.ToInt32([Link]());
if (age >= 18)
[Link]("You are eligible to vote!");
else
[Link]("Sorry, you must be 18+ to vote.");
}
}
sing System;
namespace DecisionMaking {
class Program {
static void Main(string[] args) {
/* local variable definition */
char grade = 'B';
switch (grade) {
case 'A':
[Link]("Excellent!");
break;
case 'B':
case 'C':
[Link]("Well done");
break;
case 'D':
[Link]("You passed");
break;
case 'F':
[Link]("Better try again");
break;
default:
[Link]("Invalid grade");
break;
}
[Link]("Your grade is {0}", grade);
[Link]();
}
}
}
Output
When the above code is compiled and executed, it produces the following result −
Well done
Your grade is B
using System;
namespace Loops {
class Program {
static void Main(string[] args) {
/* for loop execution */
for (int a = 10; a < 20; a = a + 1) {
[Link]("value of a: {0}", a);
}
[Link]();
}
}
}
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
[Link](i * j + "\t");
}
[Link]();
}
}
}
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
using System;
namespace Loops {
class Program {
static void Main(string[] args) {
/* local variable definition */
int a = 10;
/* while loop execution */
while (a <= 20) {
[Link]("value of a: {0}", a);
a++;
}
[Link]();
}
}
}
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20
REVERSING NUMBER
using System;
class Example {
static void Main(string[] args) {
int num = 12345, reverse = 0;
while (num != 0) {
int digit = num % 10; // Extract last digit
reverse = reverse * 10 + digit;
num /= 10; // Remove last digit
}
[Link]("Reversed Number: " + reverse);
}
}
. Counting Digits in a Number
In this example, we use a while loop to count the number of digits in an integer:
using System;
class Example {
static void Main(string[] args) {
int num = 987654, count = 0;
while (num != 0) {
num /= 10; // Remove last digit
count++;
}
[Link]("Total Digits: " + count);
}
}
Following is the output of the above code −
Total Digits: 6
ENCAPSULATRION
sing System;
namespace RectangleApplication {
class Rectangle {
//member variables
private double length;
private double width;
public void Acceptdetails() {
[Link]("Enter Length: ");
length = [Link]([Link]());
[Link]("Enter Width: ");
width = [Link]([Link]());
}
public double GetArea() {
return length * width;
}
public void Display() {
[Link]("Length: {0}", length);
[Link]("Width: {0}", width);
[Link]("Area: {0}", GetArea());
}
}
//end class Rectangle
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle();
[Link]();
[Link]();
[Link]();
}
}
}
Enter Length:
4.4
Enter Width:
3.3
Length: 4.4
Width: 3.3
Area: 14.52
sing System;
namespace RectangleApplication {
class Rectangle {
// member variables
protected double length;
protected double width;
// Constructor to initialize length and width
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
}
// Method to calculate area
double GetArea() {
return length * width;
}
// Method to display rectangle details
public void Display() {
[Link]("Length: {0}", length);
[Link]("Width: {0}", width);
[Link]("Area: {0}", GetArea());
}
}
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle(4.5, 3.5);
[Link]();
[Link]();
}
}
}
Length: 4.5
Width: 3.5
Area: 15.75
Recursive Method Call
using System;
namespace CalculatorApplication {
class NumberManipulator {
public int factorial(int num) {
/* local variable declaration */
int result;
if (num == 1) {
return 1;
} else {
result = factorial(num - 1) * num;
return result;
}
}
static void Main(string[] args) {
NumberManipulator n = new NumberManipulator();
//calling the factorial method {0}", [Link](6));
[Link]("Factorial of 7 is : {0}", [Link](7));
[Link]("Factorial of 8 is : {0}", [Link](8));
[Link]();
}
}
}
Output
When the above code is compiled and executed, it produces the following result −
Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320
using System;
namespace LineApplication {
class Line {
private double length; // Length of a line
public Line() { // constructor
[Link]("Object is being created");
}
~Line() { //destructor
[Link]("Object is being deleted");
}
public void setLength( double len ) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line();
// set line length
[Link](6.0);
[Link]("Length of line : {0}", [Link]());
}
}
}
Object is being created
Length of line : 6
Object is being deleted
sing System;
namespace OperatorOvlApplication {
class Box {
private double length; // Length of a box
private double breadth; // Breadth of a box
private double height; // Height of a box
public double getVolume() {
return length * breadth * height;
}
public void setLength( double len ) {
length = len;
}
public void setBreadth( double bre ) {
breadth = bre;
}
public void setHeight( double hei ) {
height = hei;
}
// Overload + operator to add two Box objects.
public static Box operator+ (Box b, Box c) {
Box box = new Box();
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
return box;
}
public static bool operator == (Box lhs, Box rhs) {
bool status = false;
if ([Link] == [Link] && [Link] == [Link]
&& [Link] == [Link]) {
status = true;
}
return status;
}
public static bool operator !=(Box lhs, Box rhs) {
bool status = false;
if ([Link] != [Link] || [Link] != [Link] ||
[Link] != [Link]) {
status = true;
}
return status;
}
public static bool operator <(Box lhs, Box rhs) {
bool status = false;
if ([Link] < [Link] && [Link] < [Link]
&& [Link] < [Link]) {
status = true;
}
return status;
}
public static bool operator >(Box lhs, Box rhs) {
bool status = false;
if ([Link] > [Link] && [Link] >
[Link] && [Link] > [Link]) {
status = true;
}
return status;
}
public static bool operator <=(Box lhs, Box rhs) {
bool status = false;
if ([Link] <= [Link] && [Link]
<= [Link] && [Link] <= [Link]) {
status = true;
}
return status;
}
public static bool operator >=(Box lhs, Box rhs) {
bool status = false;
if ([Link] >= [Link] && [Link]
>= [Link] && [Link] >= [Link]) {
status = true;
}
return status;
}
public override string ToString() {
return [Link]("({0}, {1}, {2})", length, breadth, height);
}
}
class Tester {
static void Main(string[] args) {
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
Box Box3 = new Box(); // Declare Box3 of type Box
Box Box4 = new Box();
double volume = 0.0; // Store the volume of a box here
// box 1 specification
[Link](6.0);
[Link](7.0);
[Link](5.0);
// box 2 specification
[Link](12.0);
[Link](13.0);
[Link](10.0);
//displaying the Boxes using the overloaded ToString():
[Link]("Box 1: {0}", [Link]());
[Link]("Box 2: {0}", [Link]());
// volume of box 1
volume = [Link]();
[Link]("Volume of Box1 : {0}", volume);
// volume of box 2
volume = [Link]();
[Link]("Volume of Box2 : {0}", volume);
// Add two object as follows:
Box3 = Box1 + Box2;
[Link]("Box 3: {0}", [Link]());
// volume of box 3
volume = [Link]();
[Link]("Volume of Box3 : {0}", volume);
//comparing the boxes
if (Box1 > Box2)
[Link]("Box1 is greater than Box2");
else
[Link]("Box1 is not greater than Box2");
if (Box1 < Box2)
[Link]("Box1 is less than Box2");
else
[Link]("Box1 is not less than Box2");
if (Box1 >= Box2)
[Link]("Box1 is greater or equal to Box2");
else
[Link]("Box1 is not greater or equal to Box2");
if (Box1 <= Box2)
[Link]("Box1 is less or equal to Box2");
else
[Link]("Box1 is not less or equal to Box2");
if (Box1 != Box2)
[Link]("Box1 is not equal to Box2");
else
[Link]("Box1 is not greater or equal to Box2");
Box4 = Box3;
if (Box3 == Box4)
[Link]("Box3 is equal to Box4");
else
[Link]("Box3 is not equal to Box4");
[Link]();
}
}
}
Output
When the above code is compiled and executed, it produces the following result −
Box 1: (6, 7, 5)
Box 2: (12, 13, 10)
Volume of Box1 : 210
Volume of Box2 : 1560
Box 3: (18, 20, 15)
Volume of Box3 : 5400
Box1 is not greater than Box2
Box1 is less than Box2
Box1 is not greater or equal to Box2
Box1 is less or equal to Box2
Box1 is not equal to Box2
Box3 is equal to Box4
INTERFACE
using System;
namespace InterfaceExample {
// Define an interface
public interface IAnimal {
void MakeSound();
}
// Implement the interface in a class
public class Dog : IAnimal {
public void MakeSound() {
[Link]("Dog barks: Woof Woof!");
}
}
class Program {
static void Main(string[] args) {
Dog myDog = new Dog();
[Link]();
[Link]();
}
}
}
Dog barks: Woof Woof!
using System;
using [Link];
namespace BugFixApplication {
//a custom attribute BugFix to be assigned to a class and its members
[AttributeUsage(
[Link] |
[Link] |
[Link] |
[Link] |
[Link],
AllowMultiple = true)]
public class DeBugInfo : [Link] {
private int bugNo;
private string developer;
private string lastReview;
public string message;
public DeBugInfo(int bg, string dev, string d) {
[Link] = bg;
[Link] = dev;
[Link] = d;
}
public int BugNo {
get {
return bugNo;
}
}
public string Developer {
get {
return developer;
}
}
public string LastReview {
get {
return lastReview;
}
}
public string Message {
get {
return message;
}
set {
message = value;
}
}
}
[DeBugInfo(45, "Zara Ali", "12/8/2012", Message = "Return type mismatch")]
[DeBugInfo(49, "Nuha Ali", "10/10/2012", Message = "Unused variable")]
class Rectangle {
//member variables
protected double length;
protected double width;
public Rectangle(double l, double w) {
length = l;
width = w;
}
[DeBugInfo(55, "Zara Ali", "19/10/2012", Message = "Return type mismatch")]
public double GetArea() {
return length * width;
}
[DeBugInfo(56, "Zara Ali", "19/10/2012")]
public void Display() {
[Link]("Length: {0}", length);
[Link]("Width: {0}", width);
[Link]("Area: {0}", GetArea());
}
}//end class Rectangle
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle(4.5, 7.5);
[Link]();
Type type = typeof(Rectangle);
//iterating through the attribtues of the Rectangle class
foreach (Object attributes in [Link](false)) {
DeBugInfo dbi = (DeBugInfo)attributes;
if (null != dbi) {
[Link]("Bug no: {0}", [Link]);
[Link]("Developer: {0}", [Link]);
[Link]("Last Reviewed: {0}", [Link]);
[Link]("Remarks: {0}", [Link]);
}
}
//iterating through the method attribtues
foreach (MethodInfo m in [Link]()) {
foreach (Attribute a in [Link](true)) {
DeBugInfo dbi = (DeBugInfo)a;
if (null != dbi) {
[Link]("Bug no: {0}, for Method: {1}", [Link], [Link]);
[Link]("Developer: {0}", [Link]);
[Link]("Last Reviewed: {0}", [Link]);
[Link]("Remarks: {0}", [Link]);
}
}
}
[Link]();
}
}
}
Length: 4.5
Width: 7.5
Area: 33.75
Bug No: 49
Developer: Nuha Ali
Last Reviewed: 10/10/2012
Remarks: Unused variable
Bug No: 45
Developer: Zara Ali
Last Reviewed: 12/8/2012
Remarks: Return type mismatch
Bug No: 55, for Method: GetArea
Developer: Zara Ali
Last Reviewed: 19/10/2012
Remarks: Return type mismatch
Bug No: 56, for Method: Display
Developer: Zara Ali
Last Reviewed: 19/10/2012
Remarks:
Get Properties, Methods, and Fields
using System;
using [Link];
class Student {
public string Name { get; set; } = "Aarav";
public int RollNo { get; set; } = 101;
public string School = "Delhi Public School";
public void Introduce() {
[Link]($"Hi, I am {Name}, Roll No: {RollNo}.");
}
}
class Program {
static void Main() {
Type type = typeof(Student);
[Link]("Properties:");
foreach (var prop in [Link]()) {
[Link]([Link]);
}
[Link]("Methods:");
foreach (var method in [Link]([Link] | [Link] |
[Link])) {
[Link]([Link]);
}
[Link]("Fields:");
foreach (var field in [Link]([Link] | [Link] |
[Link])) {
[Link]([Link]);
}
}
}
Properties:
Name
RollNo
Methods:
Introduce
Fields:
School
Invoke Method Dynamically
using System;
using [Link];
class Greeter {
public void SayHello(string name) {
[Link]($"Hello, {name}!");
}
}
class Program {
static void Main() {
Type type = typeof(Greeter);
object obj = [Link](type);
MethodInfo method = [Link]("SayHello");
[Link](obj, new object[] { "Ravi" });
}
}
Get Assembly Info
using System;
using [Link];
class Program {
static void Main() {
Assembly assembly = [Link]();
[Link]("Assembly Full Name: " + [Link]);
[Link]("Defined Types:");
foreach (var t in [Link]) {
[Link]([Link]);
}
}
}
Assembly Full Name: main, Version=[Link], Culture=neutral, PublicKeyToken=null
Defined Types:
Program
Passing Parameters to Threads
using System;
using [Link];
class Program
{
static void Main()
{
Thread thread = new Thread(() => PerformTask("Hello from thread!"));
[Link]();
[Link]("Main thread is running.");
}
static void PerformTask(string message)
{
[Link](message);
}
}
Hello from thread!
Main thread is running.