473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Passing zero (int) to an enum in .NET Socket constructor?!

This code compiles (this is how it should work):
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

This code does not compile (passing an int instead of an AddressFamily is
bad):
Socket sock = new Socket(1, SocketType.Stream, ProtocolType.Tcp);

But why on earth does not compile:
Socket sock = new Socket(0, SocketType.Stream, ProtocolType.Tcp);

Is the number zero some kind of special case here? Why is zero accepted when
the parameter type clearly says that an AddressFamily enum value is expected?!
regards,
martin
Apr 5 '06 #1
1 1770

martin wrote:
This code compiles (this is how it should work):
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

This code does not compile (passing an int instead of an AddressFamily is
bad):
Socket sock = new Socket(1, SocketType.Stream, ProtocolType.Tcp);

But why on earth does not compile:
Typo, I assume.
Socket sock = new Socket(0, SocketType.Stream, ProtocolType.Tcp);

Is the number zero some kind of special case here? Why is zero accepted when
the parameter type clearly says that an AddressFamily enum value is expected?!


Yes, zero is a special case. Here is a shorter example:

System.Net.Sockets.AddressFamily af1 = 1; // no compile
System.Net.Sockets.AddressFamily af1 = 0; // compile

The reason can be found in section 6.1.3 of the C# spec:

<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_6_1_2.asp>

(sic, the url says 6.1.2 but the page is 6.1.3 :))
C# Language Specification

6.1.3 Implicit enumeration conversions
An implicit enumeration conversion permits the decimal-integer-literal
0 to be converted to any enum-type.


Elsewhere in the docs, we are also told "The default value of an enum E
is the value produced by the expression (E)0."

--
Larry Lard
Replies to group please

Apr 5 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Clive | last post by:
Hello everyone, This is my first time posting, up until now i've only used google to seach :) I'm creating a multiplayer minesweeper game in Windows, so I have been reading up on CSockets. I...
20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
3
by: Schorschi | last post by:
Can someone please point out why I am getting an 87 error? I am sure it is obvious, but I am new to C# and seem to be having a lot of stress understanding managed versus unmanaged code when API...
0
by: Jeffrey B. Holtz | last post by:
Has anyone used the multimedia timere timeSetEvent in C#? I'm trying to use it to get a 1ms accurate timer. All the other implementations are far to inaccurate in their resolution. 1ms Timer =...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
1
by: Steve Richter | last post by:
when I call a win32 api like CreateFile, I would like to be able to pass an enumerated value as the parameter value: hFile = CreateFile( InPath, GENERIC_READ, ShareMode.Read, // this is an enum...
2
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept...
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.