Hi,
Given the following code example.
public string AddSomethingMethod(string templateName)
return GetString(new object[] { templateName });
From what I know, object[] is an array of objects.
However the reference being passed in is of type string (singular) and not an array of strings.
The GetString method accepts a parameter of params object[] args.
And the logic does the following:-
for(int i = 0; i < args.Length; i++)
string str1 = args[i] as string;
if(str1 != null) || str1.Length > 0x400
args[i] = str1.Substring(0, 0x3fd) + "...";
return args;
From this I gather that the string being passed in, is broken down into chars hence why the object array is used?
Regards
Steven
Loading
VulpesPosted Jul 26, 2012, 5:26 AM
VulpesPosted Jul 29, 2012, 2:29 PM
Guest UserPosted Jul 29, 2012, 4:32 AM
And in this case where the expression new object[] { templateName } is being used instead of a reference variable, then a completely new object\instance reference will be created? thus why returning args in my original code snippet would make sense as there is no direct reference between the two object\instance variables anymore?
VulpesPosted Jul 29, 2012, 4:23 AM
Suppose you do this using a variable rather than an expression. Then, both the parameter and the original variable will refer to the same object.
So, if you've made any changes to that object within the method, those changes will also apply to the object referred to by the original variable as it's the same object.
Guest UserPosted Jul 28, 2012, 6:56 PM
Could you slightly elaborate on this older post please...
Now, if you had passed an object variable to GetString, then that variable would still point to the same object as it did before the method was called, inclusive of the changes.
This I assume refers to passing a value type object "by value"?
Regards
VulpesPosted Jul 28, 2012, 12:16 PM
In fact, you can always tell immediately in C# whether a parameter is being passed by reference or not because, if it's the latter, then it must be preceded by 'ref' (or 'out') at the call site as well as within the method's signature.
Also, for a parameter to be passed by reference, you must use a variable - you can't use a literal.
Guest UserPosted Jul 27, 2012, 6:33 PM
One last ask on this topic if I may please:-
Given a generic statement like the following:-
List
Which category for "Passing Parameters" would this fall under? as the SomeName value has been explicitly entered when the GetInitializedList method is invoked.
Given the "SomeName" value of type string (reference-type) is passed then it would be reference-type by value?
Thanks
Guest UserPosted Jul 27, 2012, 5:49 AM
VulpesPosted Jul 27, 2012, 5:32 AM
Passing a reference-type with the ref modifier declared
Not only are any changes to the object in the called method persisted in the calling method but you can even change to a different object entirely.
Difference between out and ref
Out is the same as ref, except that the variable passed in need not have been assigned to at that time but the method guarantees that it will have been assigned to by the time the method returns.
Guest UserPosted Jul 27, 2012, 5:14 AM
Just for my understanding and\or clarification:-
All arguements in C# are passed "by value" unless otherwise stated with the ref modifier.
Passing by value-type
When these type of arguements are passed "by value" a copy of the value is created when passed to the method.
Passing a reference-type
When these type of arguements are passed " by value" a copy of the reference is created and not the actual value. Therefore any changes made in the called method are not reflected in the calling method.
With the ref modifier declared
Changing a value in the called method, changes the value in the calling method.
With regards to the AddSomethingMethod I changed the Return type to be void.
I used a try\catch for when attempting to add the templateName parameter to a collection.
and finally, used a throw new Exception, passing in the GetString method which takes the new object[] { templateName } expression.
The GetString method has a Return type of string and returns a string.Format, which has the "received" args parameter reference passed to it.
Regards
Steven
VulpesPosted Jul 26, 2012, 10:49 AM
Guest UserPosted Jul 26, 2012, 7:22 AM
Ill look into it further.
Could you elaborate on the following please:-
"there's no need to return 'args' anyway because (as it's a reference type) any changes made to it will automatically be persisted to the calling code in any case"
Regards