简体   繁体   中英

C# Interact with variable of a class in an other class

I'm learning C# and I asked the best method to interact with variable of a class out of this class. I thought about this:

public class Character
    {
        private int x, y;

        public Character(int posX, int posY)
        {
            x = posX;
            y = posY;
        }

        public int X
        {
            get
            { 
                return x;
            }
            set
            { 
                x = value;
            }
        }

        public int Y
        {
            get
            { 
                return y;
            }
            set
            { 
                y = value;
            }
        }
    }`

    class MainClass
    {
        public static void Main (string[] args)
        {
            Character hero = new Character (42, 36);
            Console.WriteLine (hero.X);
            Console.WriteLine (hero.Y);
            hero.X = 5;
            Console.WriteLine (hero.X);
        }
    }

I don't know if this method is good or optimized but it works. But, if I want do this for 10 variables, my class will do minimum (more if I want add a test in get/set) 100 lines just for my variables... Do you know an other method to proceed? Or how I can compress this method? Thank you!

You can use auto-implemented properties :

public class Character
{
   public Character(int x, int y)
   {
      X = x;
      Y = y;
   }

   public int X { get; set; }
   public int Y { get; set; }
}

Also in real life if you need to update lot of variables of other class consider to redesign your code. Probably you have separated data and logic into two different classes. Consider to combine two classes into one with data and logic which process that data.

NOTE: there is big update comming to next versions of C# called Record Types which will allow you just list all class properties after class name:

class Character(int X, int Y);

That is good syntax sugar for Data Transfer Objects.

this kind of variables is called property, it used when I want the variable to be readable and editable from other objects, so it is best solution to your issue, even if you'll increase the number of properties or add checking in get/set. go on!

In addition to auto-properties, you can use Object initializer . In that case you do not need to declare a constructor explicitly. You can save few more lines of code, using that feature . Check the code below to see the changes:

public class Character
{       
   public int X { get; set; }
   public int Y { get; set; }
}


class MainClass
{
    public static void Main (string[] args)
    {
        var hero = new Character {X = 42, Y = 36};
        Console.WriteLine(hero.X);
        Console.WriteLine(hero.Y);
        hero.X = 5;
        Console.WriteLine (hero.X);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM