DIFFERENCE BETWEEN CONVERT.TOSTRING() AND TOSTRING()
WHY SHOULD YOU OVERRIDE EQUALS METHOD
namespace ConsoleApplication4
{
public class MainClass
{
public static void Main()
{
Customer C1 = new Customer();
C1.FirstName = “Malla”;
C1.LastName = “Gurram”;
Customer C2 = new Customer();
C2.FirstName = “Malla”;
C2.LastName = “Gurram”;
Console.WriteLine(C1 == C2);
Console.WriteLine(C1.Equals(C2));
Console.ReadLine();
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override bool Equals(object obj)
{
if(obj == null)
{
return false;
}
if(!(obj is Customer))
{
return false;
}
return this.FirstName == ((Customer)obj).FirstName &&
this.LastName == ((Customer)obj).LastName;
}
public override int GetHashCode()
{
return this.FirstName.GetHashCode() ^ this.LastName.GetHashCode();
}
}
}
WHY SHOULD WE OVERRIDE TOSTRING METHOD
REFLECTION WITH SAMPLE EXAMPLE IN C#
This is a simple windows application to display the methods, properties and constructors using “Reflections” in c#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
namespace ReflectionDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDiscoverTypeInformation_Click_Click(object sender, EventArgs e)
{
string TypeName = txtTypeName.Text;
Type T = Type.GetType(TypeName);
lstMethods.Items.Clear();
lstProperties.Items.Clear();
lstConstructors.Items.Clear();
MethodInfo[] methods = T.GetMethods();
foreach(MethodInfo method in methods)
{
lstMethods.Items.Add(method.ReturnType.Name + ” ” + method.Name);
}
PropertyInfo[] properties = T.GetProperties();
foreach (PropertyInfo property in properties)
{
lstProperties.Items.Add(property.PropertyType.Name + ” ” + property.Name);
}
ConstructorInfo[] constructors = T.GetConstructors();
foreach (ConstructorInfo constructor in constructors)
{
lstConstructors.Items.Add(constructor.ToString());
}
}
}
}