c # instance of classes doubt

0
introducir el código aquí

 class Program
 {
    static void Main(string[] args)
    {

        myclass obj= new myclass
        {
            I = 1234,
            Ia = "string text"


        };
        Console.WriteLine(nuevo.Ia);// output "string text"
        Console.ReadLine();
    }
}
 class myclass
{
    private int i;
    private string ia;
    public myclass() { }


    public int I { get => i; set => i = value; }
    public string Ia { get => ia; set => ia = value; }
}

my question about what it's called and how it works when I do this

introducir el código aquí
class obj = new class 
{ /*propiedades.....?*/       }

as it would be called that operation or declaration or what exactly I am doing ... when instantiating the class and using the keys {} Could you declare new properties for the object when I put them inside the keys {} or just give values to the properties? () and {} when using {} what would it be called? PostScript: The example code works correctly .net 4.6

    
asked by Andy Macias 30.11.2017 в 16:41
source

3 answers

0

when instances and you give values with {} just enter each of the properties

var p = new Persona
  {
     Nombre = "Harry",
     edad = 20
  };

and when you do it with parentheses it does the same function without having to indicate the property if not the order of the properties

var p = new Person("Harry"); //aca lo toma como si fuera parametro

I think that clears up your doubt a bit link

    
answered by 30.11.2017 / 17:01
source
0

Trying to answer your question the answer is NO You can not add new properties to an object of a defined class when you start an instance of that object, for that you would have to create a class that extends of another class to inherit the properties and now if you can create new properties, to be more clear, is how to create an object of the class Carro we know that it has four wheels and we want to add another property to an object of that class that for example have six wheels we can not define how a car, we would have to define how another object and may inherit some characteristics of the car class.

    
answered by 30.11.2017 в 17:02
0
public class obj= new class
{
    I = 1234,
    Ia = "string text"


};
Console.WriteLine(obj.Ia);// output "string text"
Console.ReadLine();

} you just had to put the class with the public access modifier and use the name with which you installed the object

    
answered by 30.11.2017 в 17:01