How to use a variable reference in C #

I would like to shorten the expression like

this.GdkWindow.GetPosition(ConfigurationService.ApplicationSettings.rect1.X, ConfigurationService.ApplicationSettings.rect1.Y); 

to something like this.GdkWindow.GetPosition(refRect1.X, refRect1.Y) , using the link to the ConfigurationService.ApplicationSettings.rect1 variable.

In C ++, I can use a reference to an object (or variable) as follows:

 Rect& refRect1 = very.very2.very3.long_variable.rect1; GetPosition(refRect1.x, refRect1.y); 

How can I do the same in C #?

And how can I make a reference to an int variable, as the following C ++ code does:

 int A = 5; int& B = A; B = 12; 

Update: The GetPosition method GetPosition declared as void GetPosition(out int x, out int y) . This method populates the ConfigurationService.ApplicationSettings.rect1 structure with some data. That is why I would like to use the link.

I know I can do something like:

  var rect = new Gdk.Rectangle(); this.GdkWindow.GetPosition(out rect.X, out rect.Y); this.GdkWindow.GetSize(out rect.Width, out rect.Height); ConfigurationService.ApplicationSettings.rect1 = rect; 

but is there a way to avoid using a temporary variable? Is C # a lack of function even PHP? Absolutely disappointed in C #!

+4
source share
3 answers

This should work if rect1 is Rect ( var keyword did not like, but that's it):

 Rect r = ConfigurationService.ApplicationSettings.rect1; this.GdkWindow.GetPosition(rX, rY); 

Actually, this will create a copy of the structure, but do we care? Since you want to read only the field, you can use a copy.

(note that I'm not sure if this is a copy)

+6
source

These are all nested properties, so you cannot reduce them unless you first save them in a variable. As for long namespaces, you can do something like

 using System.Data.Long.Namespace.Here.SqlClient = SQLALIAS; 

And then use it as SQLALIAS.ClassName . Regarding the second question, in order to use pointers in .NET, you need to use them in the unsafe context and execute the project with the option "Allow unsafe code", although I'm not sure how this works or even if it compiles under Mono, which, like I suppose is used due to GdkWindow .

 unsafe { int A = 5; int* B = &A; } 

Follow your update:

You must call GetPosition this way.

 int x, y; GetPosition(out x, out y); 

As the compiler says A property, indexer or dynamic member access may not be passed as an out or ref parameter .

+3
source

I don’t think there is a way to use a temporary variable because every time you access ConfigurationService.ApplicationSettings.rect1, a copy of the Rect object is created.

If Rect.X and Rect.Y are public fields, you can do this:

 Rect r; this.GdkWindow.GetPosition(out rX, out rY); ConfigurationService.ApplicationSettings.rect1 = r; 

If these are properties, you will need:

 int x, y; this.GdkWindow.GetPosition(out x, out y); ConfigurationService.ApplicationSettings.rect1 = new Rect(x, y); 
+2
source

Source: https://habr.com/ru/post/1389173/


All Articles