Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ references Vs C# references [closed]

Tags:

c++

c#

What are the similarties/differences between a C++ reference and a C# reference ?

Edit:

I'm talking about Object references,as I'm a newbie I was unaware that such a simple line would cause ambiguity,as I have read. Whenever the term "reference" is used,its in context to Object reference,otherwise its explicitly quoted as "managed references".

I think all the people who have answered this question have got what I was trying to say and I made a comment that states clearly what I wanted to ask. I don't see any reason for a downvote,come on guys.

This question does not deserve to be closed.As newbies like me can learn from the insight a lot of experienced people have provided.

like image 766
Pavitar Avatar asked May 07 '11 08:05

Pavitar


People also ask

What is difference between reference and pointer in C?

References are used to refer an existing variable in another name whereas pointers are used to store address of variable. References cannot have a null value assigned but pointer can. A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.

Do C have references?

No, it doesn't.

What does reference mean in C?

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

Is pointer the same as reference?

Pointers: A pointer is a variable that holds the memory address of another variable. A pointer needs to be dereferenced with the * operator to access the memory location it points to. References: A reference variable is an alias, that is, another name for an already existing variable.


1 Answers

C# references are closer to C++ pointers than to C++ references.

C# references can be null and have assignment semantics similar to pointers. The difference is that they are black boxes(you can't convert them to integral types) and you can't do arithmetic on them. They also can only point to reference types.

C++ references are quite different(apart from being compiled to pointers too). They are closer to .net managed references which are used for ref parameters. But even then the assignment semantics differ.


Initialization of a reference is something quite different from assignment to it. Despite appear- ances, no operator operates on a reference.

like image 107
CodesInChaos Avatar answered Sep 20 '22 02:09

CodesInChaos