Hello All,
As always i am back with some confusion. I am asking very simple question.
"What is the difference between Struct and Class in .NET???"
I was confused because i think both of them provides you approach to create your own type then ?? what can be the difference??
So as a simple answer i found that
Struct :- Value Type
Class :- Reference Type
Here i am also putting simple code.
Now i am going to create simple structure and class.
After running this code i found following output. Please take a look.
As Struct is Value type value not changed, because it creates local copy.
And as Class is Reference type value changed, because Object Reference is Passed By VALUE.
For more information about difference between struct and class you can check the following link.
What's the difference between struct and class in .NET?
If you want to download demo please Click Here.
Please put your suggestion and reviews.
As always i am back with some confusion. I am asking very simple question.
"What is the difference between Struct and Class in .NET???"
I was confused because i think both of them provides you approach to create your own type then ?? what can be the difference??
So as a simple answer i found that
Struct :- Value Type
Class :- Reference Type
Here i am also putting simple code.
using System; namespace StructDemo { class Program { static void Main() { Book objBook = new Book() { Name = "C Language" }; Console.WriteLine(objBook.Name); ChangeBookName(objBook); Console.WriteLine(objBook.Name); Vehicle objVehicle = new Vehicle() { Name = "Kawasaki Ninja" }; Console.WriteLine(objVehicle.Name); ChangeVehicleName(objVehicle); Console.WriteLine(objVehicle.Name); Console.ReadLine(); } public static void ChangeBookName(Book objBook) { objBook.Name = "C++ Language"; } public static void ChangeVehicleName(Vehicle objVehicle) { objVehicle.Name = "R15"; } } }
Now i am going to create simple structure and class.
public struct Book { public string Name { get; set; } }
public class Vehicle { public string Name { get; set; } }
After running this code i found following output. Please take a look.
As Struct is Value type value not changed, because it creates local copy.
And as Class is Reference type value changed, because Object Reference is Passed By VALUE.
For more information about difference between struct and class you can check the following link.
What's the difference between struct and class in .NET?
If you want to download demo please Click Here.
Please put your suggestion and reviews.