Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Net.Sockets readonly annotation #51809

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public partial struct IPPacketInformation
{
private object _dummy;
private int _dummyPrimitive;
public System.Net.IPAddress Address { get { throw null; } }
public int Interface { get { throw null; } }
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? comparand) { throw null; }
public override int GetHashCode() { throw null; }
public readonly System.Net.IPAddress Address { get { throw null; } }
public readonly int Interface { get { throw null; } }
public override readonly bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? comparand) { throw null; }
public override readonly int GetHashCode() { throw null; }
public static bool operator ==(System.Net.Sockets.IPPacketInformation packetInformation1, System.Net.Sockets.IPPacketInformation packetInformation2) { throw null; }
public static bool operator !=(System.Net.Sockets.IPPacketInformation packetInformation1, System.Net.Sockets.IPPacketInformation packetInformation2) { throw null; }
}
Expand Down Expand Up @@ -757,11 +757,11 @@ public partial struct UdpReceiveResult : System.IEquatable<System.Net.Sockets.Ud
private object _dummy;
private int _dummyPrimitive;
public UdpReceiveResult(byte[] buffer, System.Net.IPEndPoint remoteEndPoint) { throw null; }
public byte[] Buffer { get { throw null; } }
public System.Net.IPEndPoint RemoteEndPoint { get { throw null; } }
public bool Equals(System.Net.Sockets.UdpReceiveResult other) { throw null; }
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? obj) { throw null; }
public override int GetHashCode() { throw null; }
public readonly byte[] Buffer { get { throw null; } }
public readonly System.Net.IPEndPoint RemoteEndPoint { get { throw null; } }
public readonly bool Equals(System.Net.Sockets.UdpReceiveResult other) { throw null; }
public override readonly bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? obj) { throw null; }
public override readonly int GetHashCode() { throw null; }
public static bool operator ==(System.Net.Sockets.UdpReceiveResult left, System.Net.Sockets.UdpReceiveResult right) { throw null; }
public static bool operator !=(System.Net.Sockets.UdpReceiveResult left, System.Net.Sockets.UdpReceiveResult right) { throw null; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ internal IPPacketInformation(IPAddress address, int networkInterface)
_networkInterface = networkInterface;
}

public IPAddress Address
public readonly IPAddress Address
{
get
{
return _address;
}
}

public int Interface
public readonly int Interface
{
get
{
Expand All @@ -44,10 +44,10 @@ public int Interface
return !(packetInformation1 == packetInformation2);
}

public override bool Equals([NotNullWhen(true)] object? comparand) =>
public override readonly bool Equals([NotNullWhen(true)] object? comparand) =>
comparand is IPPacketInformation other && this == other;

public override int GetHashCode()
public override readonly int GetHashCode()
{
return unchecked(_networkInterface.GetHashCode() * (int)0xA5555529) +
(_address == null ? 0 : _address.GetHashCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,9 @@ private enum QueueState : int
// never around any external call, e.g. OS call or user code invocation.
private object _queueLock;

private LockToken Lock() => new LockToken(_queueLock);
private readonly LockToken Lock() => new LockToken(_queueLock);

public bool IsNextOperationSynchronous_Speculative => _isNextOperationSynchronous;
public readonly bool IsNextOperationSynchronous_Speculative => _isNextOperationSynchronous;

public void Init()
{
Expand Down Expand Up @@ -1216,7 +1216,7 @@ public bool StopAndAbort(SocketAsyncContext context)
}

[Conditional("SOCKETASYNCCONTEXT_TRACE")]
public void Trace(SocketAsyncContext context, string message, [CallerMemberName] string? memberName = null)
public readonly void Trace(SocketAsyncContext context, string message, [CallerMemberName] string? memberName = null)
{
string queueType =
typeof(TOperation) == typeof(ReadOperation) ? "recv" :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ namespace System.Net.Sockets
{
public struct SocketInformation
{
public byte[] ProtocolInformation { get; set; }
public SocketInformationOptions Options { get; set; }
public byte[] ProtocolInformation { readonly get; set; }
public SocketInformationOptions Options { readonly get; set; }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically these aren't needed, the docs state:

The compiler declares a get accessor of an auto-implemented property as readonly, regardless of presence of the readonly modifier in a property declaration.

However, it isn't harmful or incorrect.

internal void SetOption(SocketInformationOptions option, bool value)
{
if (value) Options |= option;
else Options &= ~option;
}

internal bool GetOption(SocketInformationOptions option)
internal readonly bool GetOption(SocketInformationOptions option)
{
return (Options & option) == option;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public UdpReceiveResult(byte[] buffer, IPEndPoint remoteEndPoint)
/// <summary>
/// Gets a buffer with the data received in the UDP packet
/// </summary>
public byte[] Buffer
public readonly byte[] Buffer
{
get
{
Expand All @@ -48,7 +48,7 @@ public byte[] Buffer
/// <summary>
/// Gets the remote endpoint from which the UDP packet was received
/// </summary>
public IPEndPoint RemoteEndPoint
public readonly IPEndPoint RemoteEndPoint
{
get
{
Expand All @@ -60,7 +60,7 @@ public IPEndPoint RemoteEndPoint
/// Returns the hash code for this instance.
/// </summary>
/// <returns>The hash code</returns>
public override int GetHashCode()
public override readonly int GetHashCode()
{
return (_buffer != null) ? (_buffer.GetHashCode() ^ _remoteEndPoint.GetHashCode()) : 0;
}
Expand All @@ -70,15 +70,15 @@ public override int GetHashCode()
/// </summary>
/// <param name="obj">The object to compare with this instance</param>
/// <returns>true if obj is an instance of <see cref="UdpReceiveResult"/> and equals the value of the instance; otherwise, false</returns>
public override bool Equals([NotNullWhen(true)] object? obj) =>
public override readonly bool Equals([NotNullWhen(true)] object? obj) =>
obj is UdpReceiveResult other && Equals(other);

/// <summary>
/// Returns a value that indicates whether this instance is equal to a specified object
/// </summary>
/// <param name="other">The object to compare with this instance</param>
/// <returns>true if other is an instance of <see cref="UdpReceiveResult"/> and equals the value of the instance; otherwise, false</returns>
public bool Equals(UdpReceiveResult other)
public readonly bool Equals(UdpReceiveResult other)
{
return object.Equals(_buffer, other._buffer) && object.Equals(_remoteEndPoint, other._remoteEndPoint);
}
Expand Down