You are on page 1of 3

Question : Create a program in c# to execute static and instance String

methods.
Co-1 BL- 6

using System;

class Program
{
static void Main(string[] args)
{
// Static method example
string str_0 = "Anshul";
string str_1 = "anshul";

bool staticMethodResult = String.Equals(str_0,str_1);


Console.WriteLine("Result of static Equals is : " + staticMethodResult);

string lowerCaseString = "HELLO WORLD";


string lowerCasedString = lowerCaseString.ToLower();
Console.WriteLine("Original string: " + lowerCaseString);
Console.WriteLine("Result of instance method ToLower: " + lowerCasedString);

string upperCaseString = "hello world";


string upperCasedString = upperCaseString.ToUpper();
Console.WriteLine("Original string: " + upperCaseString);
Console.WriteLine("Result of instance method ToUpper: " + upperCasedString);

string replacedString = "Hello World".Replace("Hello", "Hi");


Console.WriteLine("Result of instance method Replace: " + replacedString);

Console.WriteLine("name: Anshul pundir\nroll no: 13\nclass MCA II(B)");


// Wait for user input to exit
Console.WriteLine("\nPress any key to exit...");

Console.ReadKey();
}
}
Question : Create a program in c# to show difference between struct and
class
Co-1 BL- 6
using System;

// Define a struct
struct MyStruct
{
public int x;
}

// Define a class
class MyClass
{
public int x;
}

class Program
{
static void Main(string[] args)
{
// Create an instance of the struct
MyStruct structInstance = new MyStruct();
structInstance.x = 5;

// Create an instance of the class


MyClass classInstance = new MyClass();
classInstance.x = 5;
Console.WriteLine("Value of structInstance.x befor ModifyStruct: " +
structInstance.x);
Console.WriteLine("Value of classInstance.x befor ModifyClass: " +
classInstance.x);
// Modify values
ModifyStruct(structInstance);
ModifyClass(classInstance);

// Print modified values


Console.WriteLine("Value of structInstance.x after ModifyStruct: " +
structInstance.x);
Console.WriteLine("Value of classInstance.x after ModifyClass: " +
classInstance.x);
Console.WriteLine("name: Anshul pundir\nroll no: 13\nclass: MCA II(B)");
}

// Method to modify struct


static void ModifyStruct(MyStruct s)
{
s.x = 10;
}

// Method to modify class


static void ModifyClass(MyClass c)
{
c.x = 10;
}
}

You might also like