|
If you call MyMethod with something that has a compile-time type of IEnumerable<T> , you'll call the correct method.
If you call it with something where the compile-time type implements IEnumerable<T> , you'll call the wrong method.
MyMethod(Enumerable.Range(1, 42));
MyMethod(new[] { 1, 2, 3 });
MyMethod(new List<int> { 1, 2, 3 });
One workaround might be to add .AsEnumerable() to the method argument.
MyMethod(Enumerable.Range(1, 42));
MyMethod((new[] { 1, 2, 3 }).AsEnumerable());
MyMethod((new List<int> { 1, 2, 3 }).AsEnumerable());
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard, using these tests with the code I posted here, I believe 'AsEnumerable may not be necessary:
var t0 = DoSomething(
t => t * 3,
100).ToList();
var t1 = DoSomething(
t => t * 3,
1,2,3,4,5).ToList();
var t2 = DoSomething(
t => t * 3, Enumerable.Range(6, 10)).ToList();
var t3 = DoSomething(
t => t * 3, new[] { 11, 12, 13 }).ToList();
var t4 = DoSomething(
t => t * 3, new List<int> { 14, 15, 16 }).ToList(); Of course, I am assuming J's choice of using the same method name (overloading) is rational, and that it implies one instance of T is handled the same way as multiple instances.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
If you start with the methods from John's post, you'll see the issue:
public static void Foo<T>(T value) => Console.WriteLine("Foo a single value");
public static void Foo<T>(IEnumerable<T> values) => Console.WriteLine("Foo multiple values");
public static void Bar()
{
Foo(42);
Foo(Enumerable.Range(1, 42));
Foo(new[] { 1, 2, 3 });
Foo(new List<int> { 1, 2, 3 });
Foo((new[] { 1, 2, 3 }).AsEnumerable());
Foo((new List<int> { 1, 2, 3 }).AsEnumerable());
} Generic Overload Resolution | C# Online Compiler | .NET Fiddle[^]
The question was, given an x whose compile-time type implements IEnumerable<> , is there a way to make Foo(x) call Foo(IEnumerable<T>) instead of Foo(T) , without modifying the call-site.
The answer seems to be that there isn't. If the compile-time type of x is not exactly equal to IEnumerable<> , then the compiler prefers to call the single-value method, using the compile-time type as the type parameter, rather than casting to IEnumerable<T> and calling the multi-value method.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard, Maybe the 103F+ days, and air pollution over-the-toxic-top, and "sheltering in place," and ... gosh: old age ? ... are creeping up on me , so I am denser than usual, but ...
doesn't the code I posted (and the tests) demonstrate a working solution with only one overload to the issue here ?
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
<crickets>
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
Wed. ... GMT +7 ... 105F at 4PM
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Yikes!
10°C / 50°F here at 1PM. Last night went down to -1°C / 30°F.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Not as far as I can see.
public static void Foo<T>(IEnumerable<T> value) => Console.WriteLine($"Foo<{typeof(T)}>(IEnumerable<{typeof(T)}>)";
public static void Foo<T>(params T[] value) => Console.WriteLine($"Foo<{typeof(T)}>(params {typeof(T)}[])";
public static void Bar()
{
Foo(42);
Foo(Enumerable.Range(1, 42));
Foo(new[] { 1, 2, 3 });
Foo(new List<int> { 1, 2, 3 });
} Generic Overload Resolution 2 | C# Online Compiler | .NET Fiddle[^]
If you pass a List<> to the method, it calls the params overload passing the entire list as the first parameter. The type parameter will be List<> , not the type of the list's elements.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yep, all I had to do was:
MyMethod(data.AsEnumerable());
The data object is indeed a List<T> , and I thought that .Net would know how to handle it without me taking steps.
Many thanks, oh Great and Powerful Oz.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
OK, the "simple" method is to cast it:
MyMethod((IEnumerable<int>)data);
which is, of course, really ugly. So, hide it in another overload:
public void MyMethod<T>(List<T> data)
{
MyMethod<T>((IEnumerable<T>)data);
}
Truth,
James
|
|
|
|
|
Whenever I can write the excel file from stored procedure but one field size is 12 digit.so It can return exponential number.How to return these actual number?
|
|
|
|
|
|
I think that you will find it is actually returning the number. Select the cell in the spreadsheet and look at the value in the formula bar. That should have the digits in it. The issue is that the default display for a large number is as an exponential format but the display is not what is actually stored.
Try, as a counter test, writing a (say) 15 digit number in a cell and then move to another cell then look at what the cell you wrote into has as its displayed value.
Update:
You can 'see' longer numbers in non-exponential format. Right click the cell(s), select Format Cells, Number tab / Category Number, set Decimal places to 0.
modified 2-Apr-20 9:40am.
|
|
|
|
|
"C# Oracle SYSDBA connection string not able to working on windows 10? But the same connection string is working on windows 7!"
|
|
|
|
|
It's probably not the connection string (though we have no idea what string you are using, how you are using it, or where your code is running).
More likely, it's a firewall / port blocking / security issue.
Talk to your Oracle DBA and see what he says first.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Not sure what you want us to do. We can't see anything that you have done.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
I am using the below code to obtain a list of all local user accounts on the machine. But in addition to the "human" accounts, it returns all the other accounts, such as the SQLSERVER accounts and something called the WDAGUtilityAccount .
What is the property I should be checking for to determine which listed accounts are the "human" accounts, and which are machine accounts?
public static List<string> GetComputerUsers()
{
List<string> users = new List<string>();
var path =
string.Format("WinNT://{0},computer", Environment.MachineName);
using (var computerEntry = new DirectoryEntry(path))
{
foreach (DirectoryEntry childEntry in computerEntry.Children)
{
if (childEntry.SchemaClassName == "User")
{
users.Add(childEntry.Name);
}
}
}
return users;
}
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hi Richard,
the first code block here[^] seems to give actual user names.
|
|
|
|
|
Hi Luc,
The first code block gives me the strings, "INTERACTIVE" and "Authenticated Users".
And the second code block fails because I do not have an LDAP server on my LAN.
Thank you
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
Hi,
Richard Andrew x64 wrote: What is the property I should be checking for to determine which listed accounts are the "human" accounts, and which are machine accounts? I believe your childEntry variable will be a IADsUser COM object[^] for end-user accounts. You may be able to perform additional filtering by inspecting the IADsUser properties[^].
I am not a C# developer so I don't know off the top of my head how you would access this. Looking at the documentation it appears that you would need to iterate through DirectoryEntry.Properties[^] to access properties like 'LastLogin'.
Best Wishes,
-David Delaune
|
|
|
|
|
Thank you David.
I'm not averse to using native code to access what I need.
Which functions would I use to do this?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Richard Andrew x64 wrote: Which functions would I use to do this?
You are the C# developer not me.
See if this works:
foreach (DirectoryEntry childEntry in computerEntry.Children)
{
if (childEntry.SchemaClassName == "User")
{
IADsUser coronavirus = childEntry.NativeObject as IADsUser;
}
}
|
|
|
|
|
Thank you for your response.
There seems to be a misunderstanding. I was asking which native functions I would use to do this. I only want the local machine accounts.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|