Linq Parameters

Introduction reference types - passing reference types as parameters

http://www.yoda.arachsys.com/csharp/parameters.html

Summary passing reference types as parameters

http://programmers.stackexchange.com/questions/155210/whats-the-point-of-passing-a-parameter-by-reference-in-c

By value

If a member of the parameter changes, the member of the original changes too.
If the 'reference' (pointer) of the parameter changes, the original reference stays.

By reference

If a member of the parameter changes, the member of the original changes too.
If the 'reference' (pointer) of the parameter changes, the original reference changes too.

Customer c1     --------------   name = "Jan"
       |                         Address  -----------  street = "Churchstreet"
Customer c by reference           |
                                  |
Customer c by value   -----------

Examples

Remark
Customer.id is an auto increment field in the database.

Insert (ref Customer c)

Customer c1 = new Customer();                      // c1.#pointer = 1000; c1.id = null
c1.name = "Jan";                                   // c1.name = "Jan"
Insert (ref c1);                                   // c1.#pointer = 1001; c1.name = "Jan"; c1.id = 1

private void Insert (ref Customer c)               // c.#pointer = 1000
{  
  DataContext dc = new DataContext();
  c = (from l in dc.spCustomer_Insert(q.name) 
       select l).single();                         // c.#pointer = 1001; c.name = "Jan"; c.id = 1
}

- The 'original' object c1 is lost. So if there were subclasses, eg. c1.Address, they will be lost after the Insert.
- The id coming back from the database is preserved.

Insert (Customer c)

Customer c1 = new Customer();                      // c1.#pointer = 1000; c1.id = null
c1.name = "Jan";                                   // c1.name = "Jan"
Insert (c1);                                       // c1.#pointer = 1000; c1.name = "Jan"; c1.id = null

private void Insert (Customer c)                   // c.#pointer = 1000
{  
  DataContext dc = new DataContext();
  c = (from l in dc.spCustomer_Insert(q.name) 
       select l).single();                         // c.#pointer = 1001; c.name = "Jan"; c.id = 1
}

- The 'original' object c1 is still there. So subclasses, eg. c1.Address, will be preserved.
- The id coming back from the database is lost.

c = Insert (Customer c)

Customer c1 = new Customer();                      // c1.#pointer = 1000; c1.id = null
c1.name = "Jan";                                   // c1.name = "Jan"
c1 = Insert (c1);                                  // c1.#pointer = 1001; c1.name = "Jan"; c1.id = 1

private Customer Insert (Customer c)               // c.#pointer = 1000
{  
  DataContext dc = new DataContext();
  c = (from l in dc.spCustomer_Insert(q.name) 
       select l).single();                         // c.#pointer = 1001; c.name = "Jan"; c.id = 1
  return c;
}

- The 'original' object c1 is lost. So if there were subclasses, eg. c1.Address, they will be lost after the Insert.
- The id coming back from the database is preserved.
- Actually, this is the same as InsertCustomer (ref c)

c1 = Insert (Customer c)

Customer c1 = new Customer();                      // c1.#pointer = 1000; c1.id = null
c1.name = "Jan";                                   // c1.name = "Jan"
Customer c2 = Insert (c1);                         // c1.#pointer = 1000; c1.name = "Jan"; c1.id = null
                                                   // c2.#pointer = 1001; c2.name = "Jan"; c2.id = 1
private Customer Insert (Customer c)                   // c.#pointer = 1000
{  
  DataContext dc = new DataContext();
  c = (from l in dc.spCustomer_Insert(q.name) 
       select l).single();                         // c.#pointer = 1001; c.name = "Jan"; c.id = 1
  return c;
}

- The 'original' object c1 is still there. So subclasses, eg. c1.Address, will be preserved.
- The id coming back from the database is preserved in c2.

Some remarks on comparing objects

object1 == object2
Checks if the pointers of 2 objects are the same.

object1.Equals(object2)
Checks if the contents of 2 objects are the same.
The standard .Equals doesn't work on custom objects. So if you want to use this, it is necessary to write your own implementation.

object1.GetHashCode()
This should produce a unique code for an object.
Some testing learned me:
- Changing members in an object, doesn't change the hashcode;
- The hashcode is different every time you restart your program;
- An object passed by reference gets the same hashcode as the object in the routine.
So it looks to me that the hashcode is more like the pointer of the object.

C# .Equals(), .ReferenceEquals() and == operator
http://stackoverflow.com/questions/3869601/c-sharp-equals-referenceequals-and-operator

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License