Cannot implicitly convert type 'System.Version' to 'System.Net.HttpVersion'

Jeroen Vannevel

Consider the following code:

void Main()
{
    HttpVersion version;
    string s = "HTTP/1.1";
    version = s == "HTTP/1.1" ? HttpVersion.Version11 : HttpVersion.Version10;
}

This throws the error

Cannot implicitly convert type 'System.Version' to 'System.Net.HttpVersion'

even though System.Version is nowhere inferred and isn't even part of HttpVersion's inheritance.

Explicit casts

void Main()
{
    HttpVersion version;
    string s = "HTTP/1.1";
    version = s == "HTTP/1.1" ? (HttpVersion) HttpVersion.Version11 : (HttpVersion) HttpVersion.Version10;
}

or explicit namespace naming

void Main()
{
    System.Net.HttpVersion version;
    string s = "HTTP/1.1";
    version = s == "HTTP/1.1" ? System.Net.HttpVersion.Version11 : System.Net.HttpVersion.Version10;
}

don't make a difference.

The question is simple: why is System.Version interfering with what should be an unrelated class?

Tim

The Version10 and Version11 static fields of HttpVersion return System.Version objects, not HttpVersion objects. So it's not technically unrelated.

Now as for WHY it does that, I have absolutely no idea. But you'll note that the places where it is used (such as HttpWebRequest.ProtocolVersion) use System.Version as well. So that's what you want to be using.

To me, it kind of looks kind of like HttpVersion should have been a static class as it has no instance members that are not inherited from Object...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task'

From Dev

Cannot implicitly convert type to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable?

From Dev

Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to 'System.Web.Mvc.JsonResult'

From Dev

Cannot implicitly convert to type string to System.Net.Mail.MailAddress

From Dev

Cannot implicitly convert type 'int' to 'System.Data.DataTable'

From Dev

Cannot implicitly convert type 'frmRemote.RemoteClickHandler' to 'System.EventHandler'

From Dev

cannot implicitly convert System.Type to object

From Dev

Cannot implicitly convert type 'void' to 'System.Net.CookieContainer'

From Dev

Cannot implicitly convert type 'void' to System.Windows.Forms.MouseEventHandler

From Dev

Cannot implicitly convert type System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Entity.Infrastructure.DbQuery'

From Dev

Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost

From Dev

Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

From Dev

2 errors - Cannot implicitly convert type system.collection.generic.list<float> to float and cannot implicitly convert type float [] to float

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

From Dev

Cannot implicitly convert type 'frmRemote.RemoteClickHandler' to 'System.EventHandler'

From Dev

Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task'

From Dev

Error Cannot implicitly convert type 'string' to 'System.DateTime' on return

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable<char[]>' to 'string[]'

From Dev

Cannot implicitly convert type string to System.Drawing.Color

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:>>' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert anonymous type to System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable<System.DateTime>' to 'System.DateTime'

From Dev

asp.net error: Cannot implicitly convert type 'object' to 'System.DateTime/Boolean'. An explicit conversion exists (are you missing a cast?)

Related Related

  1. 1

    Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task'

  2. 2

    Cannot implicitly convert type to 'System.Collections.Generic.List

  3. 3

    Cannot implicitly convert type 'System.Collections.Generic.List

  4. 4

    Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

  5. 5

    Cannot implicitly convert type 'System.Linq.IQueryable?

  6. 6

    Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to 'System.Web.Mvc.JsonResult'

  7. 7

    Cannot implicitly convert to type string to System.Net.Mail.MailAddress

  8. 8

    Cannot implicitly convert type 'int' to 'System.Data.DataTable'

  9. 9

    Cannot implicitly convert type 'frmRemote.RemoteClickHandler' to 'System.EventHandler'

  10. 10

    cannot implicitly convert System.Type to object

  11. 11

    Cannot implicitly convert type 'void' to 'System.Net.CookieContainer'

  12. 12

    Cannot implicitly convert type 'void' to System.Windows.Forms.MouseEventHandler

  13. 13

    Cannot implicitly convert type System.Collections.Generic.List

  14. 14

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Entity.Infrastructure.DbQuery'

  15. 15

    Cannot implicitly convert type ('string', 'string') to System.Net.ICredentialsByHost

  16. 16

    Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

  17. 17

    Cannot implicitly convert type 'System.Linq.IQueryable

  18. 18

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

  19. 19

    2 errors - Cannot implicitly convert type system.collection.generic.list<float> to float and cannot implicitly convert type float [] to float

  20. 20

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

  21. 21

    Cannot implicitly convert type 'frmRemote.RemoteClickHandler' to 'System.EventHandler'

  22. 22

    Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task'

  23. 23

    Error Cannot implicitly convert type 'string' to 'System.DateTime' on return

  24. 24

    Cannot implicitly convert type 'System.Linq.IQueryable<char[]>' to 'string[]'

  25. 25

    Cannot implicitly convert type string to System.Drawing.Color

  26. 26

    Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:>>' to 'System.Collections.Generic.List

  27. 27

    Cannot implicitly convert anonymous type to System.Collections.Generic.List

  28. 28

    Cannot implicitly convert type 'System.Linq.IQueryable<System.DateTime>' to 'System.DateTime'

  29. 29

    asp.net error: Cannot implicitly convert type 'object' to 'System.DateTime/Boolean'. An explicit conversion exists (are you missing a cast?)

HotTag

Archive