Why does this covariance declaration compile? [duplicate] - c#

This question already has answers here:
C# variance annotation of a type parameter, constrained to be value type
(2 answers)
Closed 9 years ago.
Consider this interface:
interface Test<out T> where T : struct { }
It compiles without errors or warnings.
As discussed in this question, and mentioned in the Covariance and Contravariance FAQ:
Variance is supported only if a type parameter is a reference type.
So why does the above interface compile? It would make sense to fail (or at least warn) on the "out" keyword. I guess the question boils down to - is there any case where using out keyword in the above example makes any difference?
Update: Here's an example misleading behavior that may slip through for the unaware developer who looks at the Interface above:
typeof(IDummy).IsAssignableFrom(typeof(MyStruct)); // should return true
typeof(ITest<IDummy>).IsAssignableFrom(typeof(ITest<MyStruct>)); // returns false
If the coder isn't aware of variance not working for value types, they would expect the second line to return true - because of the out keyword - but it never will. This is exactly the bug that prompted me to ask this question...
Another example of code that would compile but produce unexpected results:
ITest<MyStruct> foo = ...;
var casted = (ITest<IDummy>)foo;
I would expect this to work (not knowing about the limitation of covariance to reference types) but it causes a System.InvalidCastException.

is there any case where using out keyword in the above example makes any difference?
Nope. You're allowed to specify out in the declaration, you just won't ever be able to actually leverage it when dealing with a specific instance of that type.
Nothing about this program doesn't work properly, so you're essentially asking for a feature request of the compiler prohibiting this behavior as it is a likely indication of a mistake on the part of the developer. The response to that request is (just like virtually any other feature request) that Microsoft either didn't consider this as an option, or if they did, determined that it wasn't worth the time and effort to actively prohibit this behavior.

Related

Why does the "out" parameter exist in C# as a language construct? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Question
Why does the "out" parameter exist in C# as a language construct?
Elaboration on the question
Why does it exist in the first place? Aren't there better language features to get the same effect one can get with the "out" parameter?
Isn't it strange to make an value type behave like a reference type?
Aren't there better ways to return multiple values from a method?
Is it a historical thing, meaning with the first versions of C# there was no way to achieve the things one can achieve with the out parameter but now there are newer features and it's just kept in the language for backwards compatibility?
What I'm not asking
I'm not asking what it does
out parameter modifier (C# Reference)
I'm not asking how it is used
https://stackoverflow.com/a/8128838/33311
What is the purpose of the "out" keyword at the caller (in C#)?
I'm not asking what the difference between "ref" and "out" is
What's the difference between the 'ref' and 'out' keywords?
I read that one should avoid its use and choose other constructs
Best practice of using the "out" keyword in C#
I haven't found any duplicates while reading the similar questions.
Expected answer format
I'd love to hear something like, "Look, here is a problem you can only solve with the use of the "out" parameter language construct and here is a code example for it...".
Or, "Look, this used to be the only way to solve the following problem ...code example..., but since C# version ... the better way to solve is like this ... code example...".
No opinions please.
The C# compiler performs definite assignment checking. That requires it to know exactly when a variable is assigned. Usually not hard to figure out, an assignment is pretty easy to see back.
But there is a corner-case is when a variable is passed by reference to another method. Does the method require that variable to be assigned before the call, then modifies it, or is it only ever going to assign it? The compiler in general cannot know, the method may live in another assembly with the method body unavailable. True for any .NET Framework assembly for example.
So you have to be explicit about it, you use ref when the method requires the parameter to be assigned before the call, out when the method only ever assigns it. Great feature btw, it eliminates an entire swath of very common bugs.
One note about other incorrect answers on this question. Data flow plays an important role in pinvoke as well, the pinvoke marshaller needs to know whether the convert any data returned by an unmanaged function. It does not pay attention to out vs ref keywords, only to the [In] and [Out] attributes. More about that gritty detail in this Q+A.
One reason would be for compatibility with e.g. Win32 APIs. Some method declarations for Win32 API calls will require the use of out and/or ref if you want to call them directly from C#.
You can find some examples of such method declarations at pinvoke.net:
http://pinvoke.net/search.aspx?search=out&namespace=[All]
Why does it exist in the first place? Aren't there better language features to get the same effect one can get with the "out" parameter?
Whats "better"? Arguing based on C#7 where things are a bit easier, is this:
public static (bool Succesful, int Value) TryParse(string s) { ... }
var parseResult = TryParse(s);
if (parResult.Succesful)
DoSomething(parResult.Result);
better than?
if (int.TryParse(s, out var i))
DoSomething(i);
Hmmm....
And before native tuple suport, you'd actually have to implement a struct/class to be able to return multiple values.... yuck. True that the out solution wouldn't be as clean either, but still a better option than having to litter your code base with tons of lightweight container classes to simply have methods return multiple values.
Isn't it strange to make an value type behave like a reference type?
Why? Even if its only for backwards compatibility or for interop, its a needed language feature.
Aslo, your mixing up things here. If you want pass by reference semantics, you'd use the ref keyword. The out keyword means that the parameter is meant to be used as a return value; the fact that it needs to be passed by reference is an implementation detail.
Aren't there better ways to return multiple values from a method?
This is basically your first question rewritten.
Is it a historical thing, meaning with the first versions of C# there was no way to achieve the things one can achieve with the out parameter but now there are newer features and it's just kept in the language for backwards compatibility?
Backward compatibility is the obvious and important reason. Also, just because there are better ways to do things doesn't necessarily mean that language features should be removed. You could then make a case for many other language constructs: for loops, goto, etc.

Dynamic variable Compilation [duplicate]

This question already has answers here:
When should one use dynamic keyword in c# 4.0?
(7 answers)
What's the difference between dynamic (C# 4) and var?
(14 answers)
Closed 6 years ago.
dynamic d = "hello";
Console.WriteLine (d.ToUpper()); // HELLO
Console.WriteLine (d.Foo()); // Compiles OK but gives runtime error
I'm reading a book an stumbled in this section, in the third line it only throws error during runtime but it will compile even though Foo method doesn't exist.
Why not check it in compile time rather than in runtime?
Edit:
What is the significance, and when can I use this concept?
With dynamic, you are instructing the compiler to ignore it and let the runtime handle it. This is super helpful when dealing with COM and other interfaces that the compiler doesn't know about.
If you want implicit types use var instead. The compiler will infer the type and keep it strongly typed.
The dynamic-keyword causes this behaviour which is intended. When making a variable dynamic you can do everything with it making it not compile-time-safe. So by making it dynamic you completely bypass the compiletime-types - that´s why you should take care when using it.
Checking the members at runtime is the whole point of dynamic though - why should it exist otherwise?
I assume you intentionally wanted the var-keyword which gives you compiletime-safety. Have a look at this point for the difference on both. As to the when to use the keyword have a look at this post.

System.Enum as a generic type parameter with constraints [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Create Generic method constraining T to an Enum
Enum type constraints in C#
Consider the following class:
public class Transition<TState>
{
public Transition ()
{
if (!typeof(TState).IsEnum)
throw (new ArgumentException("[TState] has to be of type [System.Enum]."));
}
}
Ideally, this should be declared as:
public class Transition<TState> where TState: System.Enum
{
}
The above, of course, generates a compile-time error. My question is why has that been made illegal. Most sources explain say that it is illegal but do not explain why. Any thoughts?
As Eric Lippert says that and I quote
ALL features are unimplemented until someone designs, specs, implements, tests, documents and ships the feature. So far, no one has done that for this one. There's no particularly unusual reason why not; we have lots of other things to do, limited budgets, and this one has never made it past the "wouldn't this be nice?" discussion in the language design team."

Is there a practical reason for why List<T>.IndexOf(T) does not return a nullable int? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
The method List<T>.IndexOf() returns the zero-based index of the first occurrence of item within the entire List, if found; otherwise, –1.
I'm seeing a parallel between that, and something I just read in Code Complete, which is telling me to "avoid variables with hidden meanings".
For example:
The value in the
variable pageCount might represent the number of pages printed, unless
it equals -1, in which case it indicates that an error has occurred.
Well, I don't know if the meaning is "hidden", because it's documented clearly enough, but null seems to convey better meaning to me than -1, and .HasValue reads like a much better check than > -1. As far as I can tell, List and nullable types were both introduced in C# 2.0, so I don't think the reason for retuning an int has to do with backwards compatibility. So, do you know if there was a reason, or if this was just something that someone forgot to implement, and we now have to live with that mistake forever?
List was released with the 2.0 version of the run time as was nullable T. But List implements IList which existed with the 1.0 version of the runtime which not only didn't have nullable it didn't support generics. To meet the contract of the IList interface an implementer must return -1 on indexof failure. Code complete also maintains that you must meet the contracts you agree to and therefore List.Indexof must return -1.
Answer to Comment:
By the time the 2.0 runtime with generics there were already thousands of applications written against the non generic version. That code would be allowed to function most effectively be migrated by supporting the generic interface as an extension of the non-generic interfaces. Also, image having to classes with the same name and 90% same usage but in the case of a couple of methods totally different semantics.
System.Collections.Generic.List may have been introduced in .NET 2.0, along with nullable, but the usage of -1 predates that considerably. ArrayList.IndexOf(), Array.IndexOf(), String.IndexOf(), SelectedIndex of a listbox, etc. So List<T> most likely did the same for consistency with the existing library. (And in fact even classic VB has this meaning, so it even predates .NET 1.0)
As #rerun points out, it isn't just stylistic consistency, it's actually part of the interface contract. So +1 to him.
While List<T> didn't have to follow in the footsteps of ArrayList and didn't have to implement IList, doing so made it much more useful to programmers already familiar with those.
Just a guess, but I would think it has to do with historical reasons and readability. Even though -1 is a "special value", it will never be returned in any other case. The rule you quoted in my opinion is primarily to keep you from returning values that could have more than one meaning. -1 is standard in c++ and similar languages, so it has become somewhat of an idiom across languages. Also, while hasvalue might be more readable, I think it would be best to have the same style consistently across the framework, which would require starting from the ground up. Finally, IMHO, in my limited experience, because nullable types have to be unboxed in order to use them elsewhere, they can be more trouble than they're worth, but that's just me.
I think it's for backwards compatibility, as in framework 1.1 there wasn't nullables and the API was created back then.
The reason may be the simple fact that Nullable<T> (the actual type used when you write T?) involves unwanted complexity for something as simple as an index search. For T? x doing x == null is really only syntactic sugar for x.HasValue and when it turns out it's not null you'd still have to access the actual value through x.Value.
You don't really gain anything in comparison to just returning a negative value, which is not a valid index either. In fact, you make things just more complex.
This follows the same pattern as String.IndexOf, and many other implementations and variations of IndexOf methods, not only in the .NET libraries.
Using -1 as return value is a magic number, and those should normally be avoided. However, this pattern is well known, which is clearly an advantage when you implement a library method.

Extension methods and static methods mismatch prototype matching in c# [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Having encountered a more advanced collision problem of extension and static methods I exemplified and simplified some code to:
using System;
namespace Test
{
static class EM
{
public static string To(this object o)
{
return o.GetType().ToString();
}
}
class A
{
public static string To() { return "Test.A"; }
}
class B { }
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
object o = null;
o.To();
B b = null;
b.To();
A a = null;
a.To();
A.To();
}
}
}
I got myself suprised when the .NET compilers, ranging from 3.x to 4.y failed to resolve which method should be called in the line where "a.To();" appears. Of course, I paraphrased the compiler but that's what the error message can be brought to (literally "Member 'Test.A.To()' cannot be accessed with an instance reference; qualify it with a type name instead").
I am posting this for not going into details of why the compiler alerts a programmer (already know the rules) but more to boost a discussion in what would be a near or final resolve to the problem of this certain kind (take into account inheritance) ahead of Microsoft, where a human can tell the extension/static method call matches easily, whereas the compiler even does not supply the exact programmer's mistake description or a hint.
Looking forward to hearing from 'stackoverflowers'.
First off, your "question" is explicitly a call for discussion. It will therefore be closed, because a call for discussion is not a question. StackOverflow is not a discussion site; there are any number of forums you can use to have a discussion. This is a good place to ask a question.
To clarify the situation: what is happening here is:
overload resolution is determining that the call a.To() has an applicable candidate in the form of the static method.
the fact that the method is static but the receiver is an instance does not make the candidate inapplicable. Rather, it makes it an error for overload resolution to choose it as the best applicable candidate.
Extension methods are not checked at all unless there was no applicable candidate. There was an applicable candidate, albeit an illegal one, so extension methods are not checked.
I agree that this is a bit weird, but the analysis is both correct and desirable. The language designers must decide what is more likely: that the caller intended to call the static method but mis-typed "a" for "A", or that the caller intended to call some completely different method that might extend some type compatible with "a". The safer assumption is the former, and therefore we give an error.
I understand that some people might find this analysis somewhat weird. It is somewhat weird, but it does make sense. If you think these rules are arcane and confusing, you're right; this is what happens when you bolt on new rules to an existing algorithm over a period of more than a decade.
Overload resolution in C# was originally designed to handle a very simple language; "params" was the most complicated part of overload resolution. it now must handle generics, type inference, extension methods, dynamic, and named and optional arguments. All those changes have severely stressed the overload resolution algorithm because every time a new feature is added we must ensure that the old features continue to work just the same.
Regarding your specific situation: if it hurts when you do that then do not do that. It is a very bad idea to make static, instance and extension methods that have the same name. They are unlikely to do the same thing, so do not make them have the same name.
the compiler even does not supply the exact programmer's mistake description or a hint.
Nonsense. The compiler tells you exactly what is wrong: overload resolution chose a static method, and you called it with an instance as the receiver. That's the error in the program, and that's the error that is reported.

Categories