Home > Net >  changing value of variable on another scope with method in C#
changing value of variable on another scope with method in C#

Time:11-08

I have came across strange method called "Recieve()" in .NET.Sockets .Recieve() method changes buff variable without using ref or out. Can somebody explain to me pls.

Full Code:

        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        IPAddress ipAddr = IPAddress.Any;

        IPEndPoint endPoint = new IPEndPoint(ipAddr, 23000);

        server.Bind(endPoint);

        server.Listen(5);

        Socket client = server.Accept();

        Console.WriteLine("Client endpoint : "   client.RemoteEndPoint.ToString());

        byte[] buff = new byte[128];

        //this method confuses me Receive(buff)
        int numberOfRecievedBytes = client.Receive(buff);

        Console.WriteLine("Number of Recieved Bytes: "   Encoding.ASCII.GetString(buff, 0, numberOfRecievedBytes));

CodePudding user response:

Byte[] is a reference type; you are passing a reference to an array of type byte. You can mutate its contents, but not the reference itself.

  • Related