0% found this document useful (0 votes)
31 views3 pages

Anshul

The C# program demonstrates the difference between structs and classes by creating instances of each. It defines a struct and class with an integer property, creates instances of each initialized to 5, and passes the instances to methods to modify the property value to 10. It prints the property values before and after modification, showing that the struct property was passed by value and remained 5, while the class property was passed by reference and changed to 10.

Uploaded by

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

Anshul

The C# program demonstrates the difference between structs and classes by creating instances of each. It defines a struct and class with an integer property, creates instances of each initialized to 5, and passes the instances to methods to modify the property value to 10. It prints the property values before and after modification, showing that the struct property was passed by value and remained 5, while the class property was passed by reference and changed to 10.

Uploaded by

suraj nath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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