var sharedViewModel=new SharedViewModel;
var viewModel1 = new ViewModel1(ref sharedViewModel)
var viewModel2 = new ViewModel2(ref sharedViewModel)
var viewModel3 = new ViewModel3(ref sharedViewModel)
will there be a difference if i do
var viewModel1 = new ViewModel1(sharedViewModel)
var viewModel2 = new ViewModel2(sharedViewModel)
var viewModel3 = new ViewModel3(sharedViewModel)
ViewModel1,ViewModel2,ViewModel3 will be doing changes to the SharedViewModel instance, is there a difference between ref SharedViewModel and SharedViewModel in this case?
Edit:
Example of what will be inside my ViewModel1,ViewModel2,ViewModel3 classes
private SharedViewModel sharedvm;
Public ViewModel(SharedViewModel sharedViewModel)
{
sharedvm=sharedViewModel;
sharedvm.Collection.Add(new object());
}
private doSomthing()
{
sharedvm.Collection.RemoveAt(0);
}
now the question is if i add an object to my sharedvm collection, does that mean that i am adding an object to my sharedViewModel collection?
what about if i call the doSomthing method, will the changes reflect on my sharedViewModel?
Yeah there is a difference.
The value of SharedViewModel can be different because you are passing a reference to a reference (essentially the sharedViewModel variable itself) to the the ViewModel1 constructor.
So if SharedViewModel is changed in the ctor, then it will affect the variable in the calling scope.
By changed I mean in the ViewModel1 ctor you do this:
sharedViewModel = new SharedViewModel()
Perhaps, I wasn't clear enough.
If you've done C/C++ think of it this way.
object *a = new object()
object **b = &a
func(b)
But you still have a high level view because everything is done by the compiler.
EDIT:
First case
public ViewModel(SharedViewModel sharedViewModel)
{
sharedvm=sharedViewModel;
sharedvm.Collection.Add(new object());
}
versus second case
public ViewModel(ref SharedViewModel sharedViewModel)
{
sharedvm=sharedViewModel;
sharedvm.Collection.Add(new object());
}
In this scenario, there is absolutely no difference. The same collection in the heap is being modified. So what happens to the collection can be seen by the calling scope.
The difference occurs when in the first case you do this:
sharedViewModel = new SharedViewModel()
Now you are modifying a completely different object in memory, and the changes applied to that object's collection is not seen by the calling scope because the calling scope references a completely different object.
So usually you never new the argument you just passed in. But if for some reason you do new the argument, then the ref keyword is a way to circumvent the said problem.
No, because instance of SharedViewModel is a reference type sa already passed by as reference.
For me you should't use ref in this case.
Ref could be usefull if you want to pass non-reference type (eg. primitive types as int) as reference, so as a pointer.
As you could read on msdn, about ref
Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference.
Classes are already passed by reference. You don't need ref keyword at all.
Related
I'm writing a compare properties two objects of some class, for further listing differences.
Class oldObj, newObj;
//some initiation of above
Class classObject = new Class();
var objectKeys = classObject .GetType().GetProperties().ToList();
objectKeys.ForEach(key => {
var previousKeyValue = key.GetValue(oldObj);
var newKeyValue = key.GetValue(newObj);
if (!Equals) {...}
});
In special cases, newObj or oldObj can be nulls.
My problem is that, in such a case: key.GetValue(null) I'm getting CS0120 exception - "Non-static method requires a target".
Looking at PropertyInfo options (from metadata):
public object? GetValue(object? obj);
I assumed that it will be able to handle such a situation with object being null, with e.g. return null as its value.
Could you please explain if this is proper behaviour of this code, or I'm making something wrong to use null?
In such a case I would probably just before ForEach make some verification if objects are nulls and write separate compare code, handling this situation.
You are misunderstanding the nature of the parameter that is passed to GetValue().
When you pass in an object reference, it means that the reference is an instance property on an instance of an object. If you omit the object reference, you are telling the reflection api that you are trying to access a static member.
Please see the code below:
var MyViewModel = new MyViewModel();
var MyDomainModel = AutoMapper.Map<MyDomainModel>(MyViewModel);
MyDomainModel = service.DoSomething(MyDomainModel);
The MyDomainModel reference is overwritten with the return type from: service.DoSomething();
Should I be passing MyDomainModel by reference in this case or does it not really make much difference?
I am looking at a lot of code that was written like the above and I am wandering if there is a reason for it that I am not aware of.
If, as the comments indicate, DoSomething just modifies properties of the object passed in but returns the same object then you could replace
MyDomainModel = service.DoSomething(MyDomainModel);
with
service.DoSomething(MyDomainModel);
It doesn't matter because either way MyDomainModel will still refer to the same object.
We have two possible cases here.
The Original Instance is Modified
public MyDomainModel DoSomething(MyDomainModel model)
{
model.Property1 = X;
model.Property2 = Y;
return model;
}
Then you have to pass the original instance as argument but you can transform your function into a void since classes are passed by reference:
public void DoSomething(MyDomainModel model)
{
model.Property1 = X;
model.Property2 = Y;
}
var MyViewModel = new MyViewModel();
var MyDomainModel = AutoMapper.Map<MyDomainModel>(MyViewModel);
service.DoSomething(MyDomainModel);
// MyDomainModel.Property1 is set to X now...
The Original Instance is Replaced
public MyDomainModel DoSomething(MyDomainModel model)
{
// ...
return (new MyDomainModel());
}
In this case, if the method doesn't make any use of model, you can basically avoid passing it as argument:
public MyDomainModel DoSomething()
{
// ...
return (new MyDomainModel());
}
var MyViewModel = new MyViewModel();
var MyDomainModel = AutoMapper.Map<MyDomainModel>(MyViewModel);
MyDomainModel = service.DoSomething();
Otherwise, if the method creates the new instance keeping some retaining some properties of the old one, you have to keep it:
public MyDomainModel DoSomething(MyDomainModel model)
{
MyDomainModel newModel = new MyDomainModel();
newModel.Property1 = model.Property1;
return newModel;
}
var MyViewModel = new MyViewModel();
var MyDomainModel = AutoMapper.Map<MyDomainModel>(MyViewModel);
MyDomainModel = service.DoSomething(MyDomainModel);
if the DoSomething method is changing the input parameter to point to a new object then you must use ref otherwise I don't see the point of passing by ref.
edit
if as you've mentioned within the comments that you're only changing the state of the input parameter then there is no need at all to return the reference to the object that was passed in because the changes will remain intact after the method call.
in fact, whether the input parameter is being passed by ref or not there shouldn't be a reason to return the same reference of the object passed in. so, therefore, you can make the method service.DoSomething(MyDomainModel); return void.
It depends what the method does. Objects are always passed by reference in this manner of use (though simply saying "passed by reference" doesn't really go far enough to explain what happens under the hood), so if DoSomething manipulates MyDomainMidel the changes will survive when the method returns. If, however, DoSomething makes a new domain model instance and returns it, and you want to preserve it, you have to keep the returned value
Here are some examples:
//the passed in person is renamed, you don't need to capture the return value
public Person Rename(Person p){
p.Name = "John";
return p;
}
//the passed in person is not renamed, you need to capture the return value
public Person Rename(Person p){
p = new Person();
p.Name = "John";
return p;
}
//the passed in person is swapped out for a new one, you don't need to capture the return value
public Person Rename(ref Person p){
p = new Person();
p.Name = "John";
return p;
}
The last example differs from the middle one thanks to the ref keyword. You can conceive that in the case if the middle call, you have a person in the calling method, you call Rename (myPerson) and the framework creates a copy of the reference to the person object, and passes the copy reference to the called method. If the called method manipulates properties of the instance then the original instance is modified even though it's accessed via a copy reference. If the copy reference is pointed to a whole new object, then any change to properties affect the new object, not the original. The copy reference goes out of scope when the method returns and the edits are lost as e copy pointer silently disappears, hence why you need to capture the return value if you want it
In the case of the ref keyword, rather than a copy of the reference to your myPerson being passed, the original reference to the instance is passed. If the method points it to a new object instance, then when control returns to the calling method, it will find its myPerson instance has been replaced with an entirely new object. The use cases for this are narrow, and it's not a favoured way to program because it essentially gives the called method the power to manipulate things beyond its scope of responsibility; the calling method might not appreciate having its variable contents trashed and replaced by a method call. There is nearly always a way to avoid using ref and while you're unclear on the mechanics of it, it is best to avoid using it, even if it does mean you have to code like the middle example.
You code like the middle example a lot, perhaps without realising; strings are immutable, so every call to e.g, .Substring creates a new string representing the shorter sequence of characters, so you have to keep the return value. As a result, some developers fall into the habit even for methods that only manipulate properties of an existing object and never use the new keyword
Looking at this Microsoft article How to: Write a Copy Constructor (C#) and also this Generic C# Copy Constructor, wouldn't it be best/safe to use a reference to the class instance than to use a plain copy of the instance ?
public class Myclass()
{
private int[] row;
public MyClass(ref MyClass #class)
{
for(int i = 0; i<#class.row.Length;i++)
{
this.row[i] = #class.row[i];
}
}
}
What ref actually means:
void Change(SomeClass instance)
{
instance = new SomeClass();
}
void ChangeRef(ref SomeClass instance)
{
instance = new SomeClass();
}
Later...
SomeClass instance = new SomeClass();
Change(instance);
//value of instance remains unchanged here
ChangeRef(ref instance);
//at this line, instance has been changed to a new instance because
//ref keyword imports the `instance` variable from the call-site's scope
I can't see how this functionality would be useful with respect to a copy constructor.
Object by nature is reference not a value type. I do not see any good reason what extra advantage you would get doing it. But yes you might get into problems because of it, consider this -
You created an object and passed it with reference to couple of classes and those classes are now having access to the address of reference itself. Now I have got all the powers to go and change the reference itself with another object's reference. If here, another class had this object it is actually working on some stale object and other classes can not see what changes are being made and you are in chaos.
I do not see any use of doing it, rather it is dangerous. It does not sounds like a OO way of writing code to me.
The ref keyword is used when a method should be allowed to change the location of a reference. Reference types always pass their reference into a method (but the location of the reference cannot be modified via assignment). Values types pass their value.
See: Passing Parameters
Example:
void PassingByReference(List<int> collection)
{
// Compile error since method cannot change reference location
// collection = new List<int>();
collection.Add(1);
}
void ChangingAReference(ref List<int> collection)
{
// Allow to change location of collection with ref keyword
collection = new List<int>();
collection.Add(2);
}
var collection = new List<int>{ 5 };
// Pass the reference of collection to PassByReference
PassingByReference(collection);
// collection new contains 1
collection.Contains(5); // true
collection.Contains(1); // true
// Copy the reference of collection to another variable
var tempCollection = collection;
// Change the location of collection via ref keyword
ChangingAReference(ref collection);
// it is not the same collection anymore
collection.Contains(5); // false
collection.Contains(1); // false
// compare the references use the default == operator
var sameCollection = collection == tempCollection; // false
Can I somehow get a reference to the instance I am creating using object initialiser
var x = new TestClass
{
Id = 1,
SomeProperty = SomeMethod(this)
}
"this" should point to the new TestClass instance I'm creating. But it obviously refers the the instance of the class in which this code resides.
I'm not asking if this is a good way to do this.
I'm aware that I can do this like this:
var x = new TestClass {Id= x};
x.SomeProperty = SomeMethod(this);
I have a complicated scenario, in which a reference to the new instance in the object initialiser would make life easier.
Is this possible in any way?
There's no way around it, the C# specification explicitly says that "It is not possible for an object or collection initializer to refer to the object instance being initialized."
As for why it's impossible, I suspect that there's just no nice way to implement it. We want some syntactic sugar equivalent to
var temp = new TestClass();
temp.Id = 1;
temp.SomeProperty = SomeMethod(temp);
x = temp;
We just need a keyword to refer to temp within the initializer, but none is easily available. We can't use this because it already means something outside the initializer. Should SomeProperty = this.SomeMethod(this) be equivalent to temp.SomeProperty = this.SomeMethod(temp) or temp.SomeProperty = temp.SomeMethod(temp)? The second is consistent, but then what happens if we need the first?
We could try to use x, though we can only pick a name if the new object is immediately assigned to a variable. However, we now can't refer to the old value of x inside the initializer, doing the equivalent of temp.SomeProperty = SomeMethod(x).
We could reuse the value keyword from property setters. This sounds good since value already stands in for the missing parameter if you consider a property getter to be syntactic sugar for a set_SomeProperty(value) method. Using it to also refer to the missing variable in the object initializer looks promising. However, we could be creating this object inside a property setter, in which case value is already being used, and we need to be able to do temp.SomeProperty = SomeMethod(value).
It looks like we'll have to create a new keyword just for this purpose, maybe newthis. However, this is a breaking change to the language because any code that has a variable called newthis doesn't work any more. Microsoft generally needs a really good reason to introduce breaking changes, so it's better to forbid access to the object being initialized.
No, you can't use the object initializer to assign the object you're creating somewhere else - that defeats the whole point of the object initializer. The x variable doesn't get assigned until after the object initializer is completed. You'll need to assign the object, then use it in a separate statement.
var x = new TestClass {
Id = 1
};
x.SomeProperty = SomeMethod(x);
Exposing or using an object that hasn't been fully constructed is usually a very bad idea. Consider the following:
class Connection
{
internal string connectionString;
public Connection(ConnectionPool pool, string connectionString) {
this.pool = pool;
//this.connectionString = connectionString; // I moved it because I could.
this.pool.Register(this);
this.connectionString = connectionString;
this.Init();
}
private void Init() { //blah }
}
class ConnectionPool
{
public void Register(Connection c)
{
if ( this.connStrings.Contains( c.connectionString ) ) // BOOM
}
}
This is an extremely contrived example. Things can get a whole lot worse than this. The following was quite an interesting link regarding this issue:
Partially Constructed Objects
var x = new TestClass
{
Id = 1,
SomeProperty = SomeMethod(this)
}
Before the right part of this initialization is evaluated and executed, the reference to the new object is not yet made available to the code. That is done for security purposes, otherwise you could create some deadlock or endless loop with you code.
I'm trying to validate my understanding of how C#/.NET/CLR treats value types and reference types. I've read so many contradicting explanations I stil
This is what I understand today, please correct me if my assumptions are wrong.
Value types such as int etc live on the stack, Reference types live on the managed heap however if a reference type has for example has an instance variable of type double, it will live along with its object on the heap
The second part is what I am most confused about.
Lets consider a simple class called Person.
Person has a property called Name.
Lets say I create an instance of Person in another class, we'll call it UselessUtilityClass.
Consider the following code:
class UselessUtilityClass
{
void AppendWithUnderScore(Person p)
{
p.Name = p.Name + "_";
}
}
and then somewhere we do:
Person p = new Person();
p.Name = "Priest";
UselessUtilityClass u = new UselessUtilityClass();
u.AppendWithUnderScore(p);
Person is a reference type, when passed to UselessUtilityClass -- this is where I go - nuts...the VARIABLE p which is an instance of the Person reference is passed by VALUE, which means when I write p.Name I will see "Priest_"
And then if I wrote
Person p2 = p;
And I do
p2.Name = "Not a Priest";
And write p's name like below I will get "Not a Priest"
Console.WriteLine(p.Name) // will print "Not a Priest"
This is because they are reference types and point to the same address in memory.
Is my understanding correct?
I think there is some misunderstanding going on when people say All objects in .NET are passed by Reference, this doesn't jive based on what I think. I could be wrong, thats why I have come to the Stackers.
Value types such as int etc live on the stack. Reference types live on the managed heap however if a reference type has for example has an instance variable of type double, it will live along with its object on the heap
No, this is not correct. A correct statement is "Local variables and formal parameters of value type which are neither directly in an iterator block nor closed-over outer variables of a lambda or anonymous method are allocated on the system stack of the executing thread in the Microsoft implementation of the CLI and the Microsoft implementation of C#."
There is no requirement that any version of C# or any version of the CLI use the system stack for anything. Of course we do so because it is a convenient data structure for local variables and formal parameters of value type which are not directly in an iterator block or closed-over outer variables of a lambda or anonymous method.
See my articles on this subject for a discussion of (1) why this is is an implementation detail, and (2) what benefits we get from this implementation choice, and (3) what restrictions the desire to make this implementation choice drives into the language design.
http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx
http://blogs.msdn.com/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx
Person is a reference type, when passed to UselessUtilityClass -- this is where I go - nuts...
Take a deep breath.
A variable is a storage location. Each storage location has an associated type.
A storage location whose associated type is a reference type may contain a reference to an object of that type, or may contain a null reference.
A storage location whose associated type is a value type always contains an object of that type.
The value of a variable is the contents of the storage location.
the VARIABLE p which is an instance of the Person reference is passed by VALUE,
The variable p is a storage location. It contains a reference to an instance of Person. Therefore, the value of the variable is a reference to a Person. That value -- a reference to an instance -- is passed to the callee. Now the other variable, which you have confusingly also called "p", contains the same value -- the value is a reference to a particular object.
Now, it is also possible to pass a reference to a variable, which many people find confusing. A better way to think about it is when you say
void Foo(ref int x) { x = 10; }
...
int p = 3456;
Foo(ref p);
what this means is "x is an alias for variable p". That is, x and p are two names for the same variable. So whatever the value of p is, that's also the value of x, because they are two names for the same storage location.
Make sense now?
Value types such as int etc live on
the stack, Reference types live on the
managed heap however if a reference
type has for example has an instance
variable of type double, it will live
along with its object on the heap
Correct.
You can also describe it as the instance variables being a part of the memory area allocated for the instance on the heap.
the VARIABLE p which is an instance of
the Person reference is passed by
VALUE
The variable is actually not an instance of the class. The variable is a reference to the instance of the class. The reference is passed by value, which means that you pass a copy of the reference. This copy still points to the same instance as the original reference.
I think there is some misunderstanding
going on when people say All objects
in .NET are passed by Reference
Yes, that is definitely a misunderstanding. All parameters are passed by value (unless you use the ref or out keywords to pass them by reference). Passing a reference is not the same thing as passing by reference.
A reference is a value type, which means that everything that you ever pass as parameters are value types. You never pass an object instance itself, always it's reference.
When you pass a person, it is making a copy of the reference - do not confuse this with a copy of the object. In other words, it is creating a second reference, to the same object, and then passing that.
When you pass by ref (with the ref/out keyword), it is passing the same reference to the object that you are using in the caller, rather than creating a copy of the reference.
Maybe this some examples can show you differences between reference types and value types and between passing by reference and passing by value:
//Reference type
class Foo {
public int I { get; set; }
}
//Value type
struct Boo {
//I know, that mutable structures are evil, but it only an example
public int I { get; set; }
}
class Program
{
//Passing reference type by value
//We can change reference object (Foo::I can changed),
//but not reference itself (f must be the same reference
//to the same object)
static void ClassByValue1(Foo f) {
//
f.I++;
}
//Passing reference type by value
//Here I try to change reference itself,
//but it doesn't work!
static void ClassByValue2(Foo f) {
//But we can't change the reference itself
f = new Foo { I = f.I + 1 };
}
//Passing reference typ by reference
//Here we can change Foo object
//and reference itself (f may reference to another object)
static void ClassByReference(ref Foo f) {
f = new Foo { I = -1 };
}
//Passing value type by value
//We can't change Boo object
static void StructByValue(Boo b) {
b.I++;
}
//Passing value tye by reference
//We can change Boo object
static void StructByReference(ref Boo b) {
b.I++;
}
static void Main(string[] args)
{
Foo f = new Foo { I = 1 };
//Reference object passed by value.
//We can change reference object itself, but we can't change reference
ClassByValue1(f);
Debug.Assert(f.I == 2);
ClassByValue2(f);
//"f" still referenced to the same object!
Debug.Assert(f.I == 2);
ClassByReference(ref f);
//Now "f" referenced to newly created object.
//Passing by references allow change referenced itself,
//not only referenced object
Debug.Assert(f.I == -1);
Boo b = new Boo { I = 1 };
StructByValue(b);
//Value type passes by value "b" can't changed!
Debug.Assert(b.I == 1);
StructByReference(ref b);
//Value type passed by referenced.
//We can change value type object!
Debug.Assert(b.I == 2);
Console.ReadKey();
}
}
The term "pass by value" is a little misleading.
There are two things you are doing:
1) passing a reference type (Person p) as a parameter to a method
2) setting a refence type variable (Person p2) to an already existing variable (Person p)
Let's look at each case.
Case 1
You created Person p pointing to a location in memory, let's call this location x. When you go into method AppendWithUnderScore, you run the following code:
p.Name = p.Name + "_";
The method call creates a new local variable p, that points to the same location in memory: x. So, if you modify p inside your method, you will change the state of p.
However, inside this method, if you set p = null, then you will not null out the p outside the method. This behavior is called "pass by value"
Case 2
This case is similar to the above case, but slightly different. When you create a new variable p2 = p, you are simply saying that p2 references the object at the location of p. So now if you modify p2, you are modifying p since they reference the same object. If you now say p2 = null, then p will now also be null. Note the difference between this behavior and the behavior inside the method call. That behavioral difference outlines how "pass by value" works when calling methods
The specifications says nothing about where to allocate value types and objects. It would be a correct C# implementation to say allocate everything on the heap and there Atr situations where values are allocated on the heap other than those you write.
int i = 4;
Func dele = ()=> (object)i;
Will result in (a copy of) i being allocated on the heap because the compiler will make it into a member of a class eventhough it's not declared as such. Other than that you're pretty much spot on. And no everything is not passed as reference. It would be closer to the thruth to state that every parameter was passed by value but still not entirely correct (e.g. ref or out).