Assign to new pointer operator #4584
Replies: 3 comments 7 replies
-
Pointers are discouraged and very uncommon in C#. There shouln't be any shorthand for it. |
Beta Was this translation helpful? Give feedback.
-
In recent days we work havily with pointers - but not with "raw pointers" - with "ref / in / out" aka "managed pointers". One example is if you dislike the conditional ref operator. See this discussion: As I understand your propsal you want to combine: int i = 42;
int* iPtr = &i; to int* ptrToZero = &42; But this looks really weird to me - and it would work only for pure r-values / literals. You should write: int i = 42;
ref int iRef = ref i; And you wrote:
In my eyes this is not true. The new variable determines the lifetime of the variable. |
Beta Was this translation helpful? Give feedback.
-
The following looks like you're trying to get a null pointer to me: int* i = &0; I get the desire for wanting some kind of shorthand here, especially if it's something you do somewhat often. However, given that the pointer needs to point to a memory location, and the lifetime of that memory location dictates the safe lifetime of that pointer, I think it's critical that the developer explicitly declare their intent. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Yes, I know I just made a proposal earlier today, but I'm a very bored person and I think this one might be of more use
This, again, is just a suggestion from a novice programmer, feel free to disregard
Sorry in advance if this post annoys anyone, I will delete if it receives negative feedback or is found to be a duplicate/inferior version of another discussion
In C#, there is no way to concisely create a new pointer which references a new variable. In most cases, programmers create a variable and set it to the desired value, then use the
&
operator to point the desired pointer to the value. In the end, this new variable does not have any use, unless mutation of the pointed-to value is immediately required. When storing pointers as fields, or returning them from a function, this is rarely the case. As such, I feel that it would improve the programming experience when dealing with pointers to have a simple operator which does this for you.Possible Implementation
&:
or$
This operator would not be overloadable. Downsides to usage; Might cause confusion with existing operators or add too much to the complexity of c#.
&
operator, which works for literals, constants, and valuesThis operator would not be overloadable. Downsides to usage; Might cause confusion since
&0
could be confused with(int*)0
. Most viable optionnew*
orvar*
as a unary operatorDownsides to usage; Hard to implement, since both
new
andvar
have very specific uses already, both of which are quite unique, and therefore adding a meaning to either of them would likely be difficult to account for during parsing. Most aesthetically pleasing optionPossible uses
For demonstration purposes, the
new*
syntax will be used in the following examplesReasoning
The ability to create a pointer to a new variable feels like something which should exist as a built-in part of C#. C++ has ways to do it concisely, and it is not particularly prone to bugs from what I can theorize, so C# should allow it as well, both for sake of user experience and for thoroughness.
Sorry again, flf++
Beta Was this translation helpful? Give feedback.
All reactions