Tuhin Paul
Can you provide an example of a Reference Type in C#

Can you provide an example of a Reference Type in C#

By Tuhin Paul in .NET on Nov 11 2023
  • Chintan Malvaniya
    Nov, 2023 21

    class Program {// Example of a reference type: a simple classclass 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 person1Person person2 = person1;// Modifying the object through one reference affects the other referenceperson1.Age = 30;// Both references point to the same object, so both will reflect the changeConsole.WriteLine($"person1: {person1.Name}, Age: {person1.Age}");Console.WriteLine($"person2: {person2.Name}, Age: {person2.Age}");} }

    • 1
  • Muni Kumar Bada
    Dec, 2023 6

    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.

    • 0
  • Daniel Justin
    Nov, 2023 21

    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

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS