|
I tried this
public sealed class sample
{
public sample(){}
public add(object item)
{
}
public add<T>(T item)
{
PropertyInfo[] pi;
}
}
now when i created new object of it and call the first add, i end up the second add has been called
sample
sample s = new sample();
string newString = "sample string";
s.add(newString);
i try to debug it to track how the process goes but it called the add<t>(t item) method.
even in the intellisense, when i point my cursor the s.add(newString), it would say sample.add<string>(string item) something like that.
what is that happening?
i will appreciate for any help will come thank you
|
|
|
|
|
As you have a method with an object, and a method that is accepting a generic, unless you actually specify object as your type, your code will resolve to the generic. You can think of this as being because the string is a more specialized type than an object, so while it is derived from object, it is NOT of type object. So, if you want to call the first method, you would do:
object newString = "sample string";
s.add(newString); Oh, one small point, your class won't compile because you haven't specified the return type of your add methods.
|
|
|
|
|
ops i forgot the return type.
BTW thank you so much, now it is clear to me why is that happen.
|
|
|
|
|
You're welcome. I'm glad I could help.
|
|
|
|
|
Out of interest, do you know what would happen in that example if the add method took a string rather than object? I don't although I'm pretty sure I've come up against this before.
Regards,
Rob Philpott.
|
|
|
|
|
Base on Pete O'Hanlon answer, I am pretty sure that it will call the first method.
Gonna try it.
|
|
|
|
|
Could be, but its veering towards the 'undefined' where what it does now may change with different versions of compiler/framework etc.
Regards,
Rob Philpott.
|
|
|
|
|
Looking at the C# specification, section 7.5.3.2:
7.5.3.2 Better function member[ ^]
... the following tie-breaking rules are applied, in order, to determine the better function member:
- If MP is a non-generic method and MQ is a generic method, then MP is better than MQ.
- ...
In other words, if two candidate methods are found, and one is generic but the other isn't, then the non-generic method is always chosen.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Outstanding.
Regards,
Rob Philpott.
|
|
|
|
|
It would call the string based method because that was the most specialised version it could call.
|
|
|
|
|
It's a really bad idea to do this for exactly the reason you describe here: it's totally unobvious which of those two methods will be called in what context. (I think Pete is right, I think the first will be called if and only if the parameter has a compile time static type of object and not a subclass, but that's confusing and unclear.)
|
|
|
|
|
I have more than 4 year experience so can i move to consultant profile, if yes please let me know , How can i do this? Give me some some ideas.
Thanks in Advance.
|
|
|
|
|
Pramod Kumar Singh 2010 wrote: Give me some some ideas
ahm, wrong forum?
Don't mind those people who say you're not HOT. At least you know you're COOL.
I'm not afraid of falling, I'm afraid of the sudden stop at the end of the fall! - Richard Andrew x64
|
|
|
|
|
Looking at your messages in CP, ... well, sorry, ... I think you mix up "time" and "experience".
|
|
|
|
|
Apply for some positions and go from there.
|
|
|
|
|
Moving into more design-oriented roles where you are in charge of higher level abstraction rather than being the code monkey with the typewriter is about knowledge and skill, not time spent bashing keys. So you should develop skills, pick up knowledge and demonstrate within your project teams that you are capable, so you have some design and architecture experience to stick on your CV when you start hawking yourself as a consultant.
|
|
|
|
|
Considering you just asked what a "extension method" was, you do NOT have 4 years experience in C#.
|
|
|
|
|
To be fair, he doesn't actually state he has 4 years of experience in .NET. It could be in Origami.
|
|
|
|
|
|
I have some CSV files that i import and place in Datatables, from there i need to make a comparison of the rows, case is that some of them are really big files so i have to make a lot of iterations which results in a really really bad performance, does anyone have an idea of what can i do to improve the performance? i did some filters but still same result. I´m kind a new in this please help me.
|
|
|
|
|
Hi,
Can you please give a snippet of your code? Maybe, the import and the "comparison of rows".
Thanks.
Don't mind those people who say you're not HOT. At least you know you're COOL.
I'm not afraid of falling, I'm afraid of the sudden stop at the end of the fall! - Richard Andrew x64
|
|
|
|
|
Hi, this is the function where i make some of the comparison, in this case i need to find if exist a difference between 2 fields of the tables.
DataView dv = new DataView(dt1);
dv.RowFilter = "[Design Data] NOT LIKE ''";
filesSTdt = dv.ToTable();
DataView dv2 = new DataView(filesSTdt);
int contRow = 0;
bool foundflag;
string designDataL, MpStatusF, MpStatusL, MpF, MpL;
string[] designDataF;
for (int k = 0; k < dt1.Columns.Count; k++)
{
dt3.Columns.Add(dt1.Columns[k].ToString(), Type.GetType("System.String"));
}
for (int i = 0; i < dt2.Rows.Count; i++)
{
designDataL = dt2.Rows[i]["DS-PZ"].ToString();
MpL = dt2.Rows[i]["MP"].ToString();
MpStatusL = dt2.Rows[i]["Status"].ToString();
dv2.RowFilter = "[Design Data] LIKE '" + designDataL + " A' AND [Change] = '" + MpL + "'";
filesSTdt = dv2.ToTable();
if (filesSTdt.Rows.Count > 0)
{
for (int j = 0; j < filesSTdt.Rows.Count; j++)
{
designDataF = filesSTdt.Rows[j]["Design Data"].ToString().Split(' ');
MpStatusF = filesSTdt.Rows[j]["Implementation Status"].ToString();
MpF = filesSTdt.Rows[j]["Change"].ToString();
if (designDataL == designDataF[0])
{
if (MpStatusL != MpStatusF)
{
dt3.ImportRow(filesSTdt.Rows[j]);
dt3.Rows[contRow]["Implementation Status"] = MpStatusL;
contRow = contRow + 1;
}
}
}
}
}
|
|
|
|
|
I sounds like you are using the datatable in c#, a very expensive construct, either change it to a List<> or even better move the data to a database and do the heavy lifting in there using stored procedures.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
How better could be the performance using lists vs using datatables?
|
|
|
|
|
Not a question that can be answered by anyone but you.
Never underestimate the power of human stupidity
RAH
|
|
|
|