473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Array.Sort(Array arr)

It just came to my mind... The method sorts the provided array, but it doesn't require ref or out parameter (C#). How does it alter the elements of the array then? Does it use explicit pointers internally? Just wondering... Thanks.
Feb 5 '09 #1
4 1319
vekipeki
229 Expert 100+
You don't have to access internal array properties to sort it, because it's sorted "in place" (i.e. no resizing or memory allocation is being done), the original internal array is still the same object.

It also does not alter the elements of the array, it just makes the array point to those elements in a different order.
Feb 5 '09 #2
I still don't get it, sorry...
The Array.Sort() method changes the provided array object, does it not? But the parameter is passed by value, not by reference. Then, how come a parameter that is passed by value be altered after the method call returns?
Feb 6 '09 #3
vekipeki
229 Expert 100+
When you are passing a class (any class, including Array) to a method without the "ref" keyword, you are still actually passing a reference to the actual object.

Consider the following code:

Expand|Select|Wrap|Line Numbers
  1. class Program
  2. {
  3.     public class Data
  4.     {
  5.         public int a;
  6.     }
  7.  
  8.     public static void Change(Data data)
  9.     {
  10.         data.a = 20;
  11.     }
  12.  
  13.     static void Main(string[] args)
  14.     {
  15.         Data data = new Data();
  16.         data.a = 10;
  17.  
  18.         Change(data);
  19.         Console.WriteLine(data.a); // a is now 20
  20.     }
  21. }
  22.  
You can see that "data" variable is pointing to the same object during its lifetime. The "Change()" method accesses that object and really changes the "a" property, although it is not passed with a "ref" keyword.

If we had passed "data" using a "ref" keyword, we would be actually passing a reference to our "data" variable, not to the actual object. In that case, we could make "data" point somewhere else inside the "Change()" method, and it would reflect in the main program also.

So, when passing a reference value (any class instance) to a method, remember:
  • Without "ref" keyword - you are passing a reference to your object
  • With "ref" keyword - you are passing a reference to the actual variable which points to your object
Feb 6 '09 #4
I understand now. Great answer! Thanks for taking the time to explain.
Feb 9 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: soup_or_power | last post by:
Hi I have an associative array like this: arr=30; arr=20;arr=40;arr=10; I want the sort function to sort keys in ascending order of the values on the right hand side with the following result:...
1
by: strotee | last post by:
#include <iostream> #include <ctime> using namespace std; // function declarations void swap(int *a, int *b); void sort(int arr, int beg, int end); int main() {
10
by: Roy Gourgi | last post by:
Hi, How would I sort an array? TIA Roy
16
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
5
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the...
4
by: philip | last post by:
Public Class Form1 Public Structure Identity Public firstname As String Public name As String End Structure I declared a array of a structure : Private Sub Form1_Load(ByVal sender As...
4
by: Balaskas Evaggelos | last post by:
Hi, does anyone know how i can sort a multi-dimensional array by a specific field ? for example i want to sort arr where n=2, but i need the data of every array to follow that order. ...
4
by: Santosh Nayak | last post by:
Hi, Is it possible to sort the array of struct based on the data members. e.g. struct { int a ; float b ; char c ; } TEMP ;
1
by: Bob Palank | last post by:
' Given Dim v(19) As Integer ' a 20 element array Dim vSorted(19) As Integer Dim i As Integer ' The array v() is filled with random numbers
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.