- Home /
c# passing classes through functions
hi, i would like to pass a class into a function. I was wondering if a class values were changed inside the function would affect the actual class because it has not created a new instant (i could be wrong). does any one know, or would have have to return the class. Thanks
Answer by Kiwasi · Nov 23, 2014 at 12:27 AM
When passing classes you are passing by reference. Any changes will affect the original.
When passing primitives or structs you pass by value. Any changes will not affect the original.
Not meaning to be pedantic, but the post title and answer don't match, even if answering samqweqwe's question.
Presumably, your answer implies instances of the class, not the class itself - as stated in original title?
I agree, the question as written makes no sense.
You cannot "chnage a class" except by loading a new compilation.
If you mean static fields then you can reference them off the class name such as $$anonymous$$yClass.myField, with no need to pass anything.
WHEN you pass classes what you are passing really is a "type", and the only thing you can do with it is reflection based operations.
Sorry, I worded the question terribly. I meant passing an instance of a class.
Answer by Jignesh G. · Nov 23, 2014 at 06:36 PM
You can pass class like below example.
void Start()
{
Hello (new testClass);
}
void Hello(testClass tc)
{
//some code goes here
}
class testClass
{
public int dataValue;
public testClass()
{
}
}
}
Or you can pass component as a argument also if class is using monobehaviour.
Your answer
![](https://koobas.hobune.stream/wayback/20220613172222im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Not sure why my function isn't working right. 0 Answers
How to Skip Parameter in C# 2 Answers
Attach a value to an instantiate object and call it in the class of this object 0 Answers
How do I create a function that accepts EITHER vector3 OR three floats? 1 Answer
How do I use a function in a class more than once? 4 Answers