Shoutbox

C# anyone? - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: C# anyone? (/showthread.php?tid=97872)

C# anyone? by Chancer on 06-25-2011 at 01:32 AM

I'm starting to learn C# (by my own, so far, with a teacher soon).
I'd like to create a linked list able to store 2 variables.

I'm working with TCP connections, and one of these variables is obviously the client socket, which is the only one I'm storing right now:

C# code:
private LinkedList<Socket> list = new LinkedList<Socket>();
I also want to store a string, representing the client name, for example.
Is that possible?

I thought of creating a new structure ("transcoding" what I learned on C), but I'd need to use pointers, and I read somewhere C# has no pointers...

Edit: reading the reference on MSDN I found a Dictionary. Do you think replacing the linked list by the dictionary would work?
The methods I need are Add, RemoveFirst, Cound and I also use:
C# code:
foreach(Socket s in list) //cmd

I'm afraid it's not possible due to this:
C# code:
while (list.Count != 0) list.RemoveFirst(); // this is using LinkedList
I don't think I can remove one element without telling which on, right?
RE: C# anyone? by Mnjul on 06-25-2011 at 09:43 AM

You can't do a RemoveFirst on a Dictionary.

Why would you need pointers?

Anyway, http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx would suffice (just create a List<KVP<String, Socket>>, or swap String & Socket if you want the latter to be the key.).


RE: C# anyone? by Chancer on 06-25-2011 at 04:39 PM

Very interesting. I'll try that later.
Thanks.


RE: C# anyone? by Adeptus on 06-28-2011 at 04:08 AM

There is no reason why you can't use LinkedList with your own class that holds the Socket and whatever additional item(s) you want.

I think you are misinterpreting that there are "no pointers" in C# to also mean there are no references, which isn't the case.  What that means, in essence, is you can't easily do funky pointer math and you shouldn't count on being able to know how your data types are laid out on the low level.  You may wish to read this.


RE: C# anyone? by Chancer on 06-28-2011 at 07:18 PM

Interesting. Actually, since it's the first time I'm using an object-oriented language (I've "used" Python once, but I don't think that counts), this things of classes, methods, events, they are still a bit messed in my head.
I'm not really worried about it now, because soon I'll have some classes and (hopefully) this mess will be cleared. I just wanted to start coding a little bit using what I know from C and some extra new stuff.

In C, I could do this:

C code:
struct client {
    int socket_descriptor;
    char[10] name;
    struct client *next;
    struct client *prev;
}
next and prev were my problem on C#. It's easy with C pointers and I wanted to know if I could do that on C#. After reading that page I'm still not sure, maybe I'll give it a try later. But I bet using ref will be useful when I effectively start coding. Thanks.

And about my program, turns out that I was using RemoveFirst to clean the whole list, so I was able to use a Dictionary and it's method Clean.
In the end, I have a Chat server and client (like chatting online in the 90's :P), with some limitations, but I really liked the interface (everything I had coded until now was console-apps). Too bad no one is going to use it :(