Interviews3 answers
Can you provide an example of a Reference Type in C#
3 Answers
Examples of Reference Types are string, object, collections.
int a=10; //it is store the there memory and it is store the stack memory
object b=10; //it is store the reference memory and it is store the heap
//memory.
Muni Kumar BadaPosted Dec 5, 2023, 10:42 PM EST
Reference types store variable references. Examples of built in reference types include object, dynamic and string.
Other examples of references types are
MyClass obj = new MyClass();
In this case obj is a reference to an instance of MyClass
Daniel JustinPosted Nov 21, 2023, 11:01 AM EST
class Program
{
// Example of a reference type: a simple class
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
static void Main()
{
// Creating an instance of the reference type (Person)
Person person1 = new Person("John Doe", 25);
// Creating another reference (person2) pointing to the same object as person1
Person person2 = person1;
// Modifying the object through one reference affects the other reference
person1.Age = 30;
// Both references point to the same object, so both will reflect the change
Console.WriteLine($"person1: {person1.Name}, Age: {person1.Age}");
Console.WriteLine($"person2: {person2.Name}, Age: {person2.Age}");
}
}
Chintan MalvaniyaPosted Nov 21, 2023, 4:39 AM EST


