-3

I have a class Program which have an object of class c2 as shown in an example below

class c2{
    public string Name;
    public int RollNo;
    public c2(string Name , int RollNo){
        this.Name = Name;
        this.RollNo = RollNo;
    }

    private c2 obj1;
    public c2 obj{
        get
        {
            return obj1;
        }
        set{
            obj1=value;
        }
    }
    public void show(){
         Console.WriteLine(""+obj1.Name+" "+obj1.RollNo);
    }
}
public class Program
{
    public static void Main(string[] args)
    {
        //Your code goes here
        c2 objNew = new c2("Test",12);
        objNew.obj = objNew;
        objNew.show();

    }
}

OUTPUT:

Test 12

In the above code class c2 has a private reference obj1 and which is get set by an object obj from class program.How value is assigned to obj and obj1 as they are not physically existed as memory is not assigned to them with the help of new operator.

  • Not clear what you're asking. What are 'the properties of an object of a class'? – Charles Mager Apr 24 '17 at 07:48
  • Check this one, can be helpful http://stackoverflow.com/questions/4142867/what-is-difference-between-property-and-variable-in-c-sharp – Cleptus Apr 24 '17 at 07:49
  • A property is a member of an instance of a class (unless it is `static`). The same appiles for fields and methods. A property has nothing to do with a field, in particular it is no extension of the latter. – MakePeaceGreatAgain Apr 24 '17 at 07:49
  • var c1Obj = new C1(); c1Obj.obj = new C2(); //Assuming C2 has a string property called propVal c1Obj.obj.propVal = "new value" – ganeshran Apr 24 '17 at 07:49

3 Answers3

1

Yes, you can, for example

class C1{
     public C2 Obj {get;set;}
}

class C2{}

public class Program
{
    public static void Main()
    {
        C1 c1 = new C1();
        C2 prop1 = c1.Obj;
        c1.Obj = new C2();
    }
}
oleksii
  • 35,458
  • 16
  • 93
  • 163
0

They can be get or set, if they are declared that way.

Readonly property: public C2 obj {get;}

Write and read property public C2 obj {get;set;}

You can do also fancy things with properties like public C2 obj {get;private set;} or add custom validation logic in the setter method.

You got a sample in setting a property in oleksii's answer

Cleptus
  • 3,446
  • 4
  • 28
  • 34
0

Yes you can like this

class Car
{
    public string Name { get; set; }
    public Color Color { get; set; }
    public CarManufacturer Manufacturer { get; set; }
}

Car car = new Car();
car.Name = "Corvette";
car.Color = Color.Yellow;
car.Manufacturer = new CarManufacturer();
car.Manufacturer.Name = "Chevrolet";
car.Manufacturer.Country = "USA";

also you can intilize it like this

Car car = new Car { 
                Name = "Chevrolet Corvette", 
                Color = Color.Yellow, 
                Manufacturer = new CarManufacturer { 
                    Name = "Chevrolet", 
                    Country = "USA" 
                } 
            };
Omar Maher
  • 45
  • 4