//Structure and class don't adopt the same aproach taward the System.Object.Equals() method//
Suppose those Strucutre and class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
struct StructurePerson
{
public string FirstName;
public string LastName;
}
class ClassPerson
{
public string FirstName;
public string LastName;
}
//Now, try this code
class Program
{
static void Main(string[] args)
{
StructurePerson strX = new StructurePerson();
strX.LastName = "Bejaoui";
strX.FirstName = "Bechir";
StructurePerson strY = new StructurePerson();
strY.LastName = "Bejaoui";
strY.FirstName = "Bechir";
if (strX.Equals(strY))
{
Console.WriteLine("strX = strY");
}
else
{
Console.WriteLine("strX != strY");
}//This code displays strX = strY
ClassPerson clsX = new ClassPerson();
clsX.LastName = "Bejaoui";
clsX.FirstName = "Bechir";
ClassPerson clsY = new ClassPerson();
clsY.LastName = "Bejaoui";
clsY.FirstName = "Bechir";
if (clsX.Equals(clsY))
{
Console.WriteLine("clsX = clsY");
}
else
{
Console.WriteLine("clsX != clsY");
}//This code displays clsX != clsY
Console.Read();
}
}
}
//
In the first strucutre case the two objects are value types,
they are compared according to their values like int I = 5 and int J = 5
so I=J because they have the same value.
At the contrast, in the class case two different and distinct reference are created so to make clsX = clsY you should use this code.
//
---------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
struct StructurePerson
{
public string FirstName;
public string LastName;
}
class ClassPerson
{
public string FirstName;
public string LastName;
}
//Now, try this code
class Program
{
static void Main(string[] args)
{
StructurePerson strX = new StructurePerson();
strX.LastName = "Bejaoui";
strX.FirstName = "Bechir";
StructurePerson strY = new StructurePerson();
strY.LastName = "Bejaoui";
strY.FirstName = "Bechir";
if (strX.Equals(strY))
{
Console.WriteLine("strX = strY");
}
else
{
Console.WriteLine("strX != strY");
}
ClassPerson clsX = new ClassPerson();
clsX.LastName = "Bejaoui";
clsX.FirstName = "Bechir";
ClassPerson clsY = clsX;
Console.WriteLine(clsY);
Console.WriteLine(clsY.FirstName);
Console.WriteLine(clsY.LastName);
Console.WriteLine(clsY.GetType());
Console.WriteLine(clsY.ToString());
Console.WriteLine(clsY.GetHashCode());
if (clsX.Equals(clsY))
{
Console.WriteLine("clsX = clsY");
}
else
{
Console.WriteLine("clsX != clsY");
}//This code displays clsX = clsY
Console.Read();
}
}
}
//
strX=strY
consoleApplication1.ClassPerson
Bechir
Bejaouri
consoleApplication1.ClassPerson
consoleApplication1.ClassPerson
25435346
clasX=clasY
-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace structure_basics
{
struct anustr
{
public string designation;
public string firstname;
public string lastname;
public string company;
public string id;
public anustr(string firstnamemain, string lastnamemain, string companymain, string designationmain, string idmain)
{
firstname = firstnamemain;
lastname = lastnamemain;
company = companymain;
designation = designationmain;
id = idmain;
}
public string output()
{
if (id == null)
{
return "empty record";
}
else
{
string s = string.Format("employee: {0} {1} {2} {3} {4}", firstname, lastname, company, id, designation);
return s;
}
}
}
class Program
{
static void Main(string[] args)
{
anustr strct1 = new anustr("anurag", "nayak", "Hcl","software engineer","234");
//anustr strct1;
string s = strct1.output();
strct1.designation = "software anura";
strct1.firstname = "anurag";
strct1.lastname = "nayaksdfa";
strct1.company = "Hcl";
//string s = strct1.output();//output will be
Console.WriteLine(s);//
Console.WriteLine(strct1.designation);//software anura
Console.ReadLine();
}
}
}
/*Let me explain you clearly regarding this program..........
* we have defined a structure....
* in main we are passing the parametirized constructor....
* if it is like this
*
* anustr strct1 = new anustr("anurag", "nayak", "Hcl","software engineer","234");
strct1.designation = "software anura";1
strct1.firstname = "anurag";2
strct1.lastname = "nayaksdfa";3
strct1.company = "Hcl";4
string s = strct1.output();
Console.WriteLine(s);
Console.WriteLine(strct1.designation);//software anura
Console.ReadLine();
*
* output of s will not depend on the parameters passed....it will depend on lines below to that...
* 1 2 3 4
*
* if it is like this
* anustr strct1 = new anustr("anurag", "nayak", "Hcl","software engineer","234");
string s = strct1.output();
strct1.designation = "software anura";
strct1.firstname = "anurag";
strct1.lastname = "nayaksdfa";
strct1.company = "Hcl";
*
Console.WriteLine(s);
* Console.WriteLine(strct1.designation);//software anura
Console.ReadLine();
*
* output will depend on the parameters passed.........got it//////////
* but
* this line will print like this
* Console.WriteLine(strct1.designation);//software anura
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
No comments:
Post a Comment