how can i compare between two objects using getType method
i want to know if two objects are from the same class or from the same base class

To get object type

Program p = new Program();
Console.WriteLine(p.GetType().Name);//prints Program

To get object base type

Program p = new Program();
Console.WriteLine(p.GetType().BaseType.Name);//prints Object (the base class of Program class)

To compare between two objects you just need to use string.Compare....

thanks man

You're welcome, friend ;)

how about using "is" keyword to compare types?

Also tell me how will you compare generic lists using the method you told?

Don't use old threads :p

List< String >  names = new List< String >();
names.Add("Serkan");
if(names.GetType().IsGenericType)
Console.WriteLine(names[0].GetType().Name);

what you show works when there is an item in that list, what if the list is empty?

here i tested your code with example and it works, i attach the working example too, but tell me if you didnt add an item to that list, how would you understand the type?

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication13
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private class someClass
		{
		}
		private void button1_Click(object sender, EventArgs e)
		{

			someClass sc1 = new someClass();
			List<someClass> g = new List<someClass>();
			g.Add(sc1);
			if (g.GetType().IsGenericType)
				MessageBox.Show(g[0].GetType().Name);
		}
	}
}

Add item then check it, I don't have any solution right now for empty lists.

but i have a special case where i dont have an item in that list. that is why i asked. the item is added at run time, so at compile time you dont know the item. But anyway i am tired, i cant think of another solution either.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.