Sign in to follow this  
Googol PL3X

A few C# questions

Recommended Posts

I'm a C++ programmer and am learning some C# so just a few questions concerned with that. 1. Is there any point in using pointers in C#? 2. Is method just another name for function? 3. Do methods need a prototype just as functions do in C++? 4. Can a class or structure be used as a return type for a method? Thanks

Share this post


Link to post
Share on other sites
1. No, there is no point in using pointers. Just realize that a 'class' is reference counted and that value types including 'struct' are not.

2. You could say that, yes. But more specifically, a method is a function of an object like a 'class' or 'struct'.

3. C++ needs a prototype? I guess you are referring to C?
In C# you do not need prototypes, there is however the type 'interface' which you can see as being a prototype.

4. Yes...both can be used. For a 'class' a 'reference' is returned while for a 'struct' it is returned as a value.

Share this post


Link to post
Share on other sites
Codur is right for the most part. Except a Interface is not the equivalent of a prototype. A interface is a contract that a class must follow. If you inherit from a interface the interface states you must implement the methods it contains and then you can extend upon it. There are no prototypes needed in C#.

Share this post


Link to post
Share on other sites
The answers were pretty good except for the first part.

Yes C# has use for pointers, just not very often. There are times that you would needed direct memory access for performance reasons (there are some other reasons also). There are some sorting methods that are much faster using pointers then references. So yes there are uses for pointers, just need to know when to use them.

theTroll

Share this post


Link to post
Share on other sites
Quote:

1. No, there is no point in using pointers. Just realize that a 'class' is reference counted and that value types including 'struct' are not.

This is inaccurate. Reference types exist on the managed heap and are garbage collected (however, no existing production CLR implementation uses reference counting because of the cycle problem -- the SSCLI tried it and it was a disaster). Value types exist on the stack (most of the time).

Quote:

3. C++ needs a prototype? I guess you are referring to C?
In C# you do not need prototypes, there is however the type 'interface' which you can see as being a prototype.

A "prototype" in C or C++ is a function declaration. Both languages require them in certain contexts, C# does not. An interface in C#, as noted, is nothing like a prototype in C or C++.

Quote:

Yes C# has use for pointers, just not very often. There are times that you would needed direct memory access for performance reasons (there are some other reasons also). There are some sorting methods that are much faster using pointers then references. So yes there are uses for pointers, just need to know when to use them.

It's also worth noting that "knowing when to use them" includes understanding that dropping into unsafe mode to use pointers has its own performance cost, so they don't come for free.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this