Develop Dairy/C#

์ž๋™ ๊ตฌํ˜„ Property ์•Œ์•„๋ณด๊ธฐ. (2021. 05. 11)

ํ‰์ƒ์ฝ”๋”ฉ 2021. 5. 11. 10:09

๐Ÿ“–์ž๋™ ๊ตฌํ˜„ Property๋ฅผ ์•Œ์•„๋ณด์ž

  • Visual Stuidio(VS)์—์„œ๋Š” prop ๋ฅผ ํƒ€์ดํ•‘ํ•˜๊ณ  ํƒญ์„ ๋ˆ„๋ฅด๋ฉด ์ž๋™์œผ๋กœ property ๊ตฌ๋ฌธ์ด ์ƒ์„ฑ๋จ.
  • ์ž๋™์œผ๋กœ ํ”„๋กœํผํ‹ฐ get. set์ด ์•„๋ž˜์™€๊ฐ™์ด ๋งŒ๋“ค์–ด์ง€๋Š”๋ฐ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ชจ์Šต์ด ์ž๋™ ๊ตฌํ˜„ Property๋ผ๊ณ  ํ•จ
public string Id {get; set;}
public string Curriculum {get; set;}

/*์ดˆ๊ธฐ๊ฐ’์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ*/
public string Id {get; set;} = "์›ํ•˜๋Š” ๊ฐ’"
public string Curriculum {get; set;} = "์›ํ•˜๋Š” ๊ฐ’"
  • ๋ณดํ†ต public ๋ณ€์ˆ˜์—๋‚˜ ์“ฐ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ private์—์„œ๋Š” ์ž˜ ์“ฐ์ง€ ์•Š๋Š”๊ฒŒ rule์ž„.
  • e.g Blazor์—์„œ๋Š” Parameter๊ฐ’๋“ค์„ ํ”„๋กœํผํ‹ฐ๋กœ ์„ ์–ธ

  • [Parameter] public string variableName {get; set;}
     
  • get{...}, {set...}์„ ์ผ์ปฌ์–ด ์ ‘๊ทผ์ž(accessor) ๋ผ๊ณ  ํ•จ.
  • set ์ ‘๊ทผ์ž๋Š” ํ•„๋“œ(field)์— ๊ฐ’์„ ํ• ๋‹น(๋„ฃ์–ด์คŒ)
  • get ์ ‘๊ทผ์ž๋Š” ํ•„๋“œ(field)๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ์ฝ์–ด์˜ด
  • ํ•„๋“œ๋Š” "variableName"์˜ ์ž๋ฆฌ์— ์žˆ๋Š”๊ฒƒ์„ ๋งํ•จ
  • ๋งจ ์œ„์— Id, Curriculum proeprty๋“ค์„ ์•„๋ž˜์ฝ”๋“œ์ฒ˜๋Ÿผ ํ•œ ๋‹จ๊ณ„ ํ’€์–ด์„œ ์จ๋ณด๋ ค๊ณ ํ•จ
  • private string id; /* ํ•„๋“œ ๋ณ€์ˆ˜ ์„ ์–ธ*/
    private string curriculum; /* ํ•„๋“œ ๋ณ€์ˆ˜ ์„ ์–ธ*/
    
    public string Id  
    {  
        get {return id;}  
        set {id = value;}  
    }
    
    public string Curriculum  
    {  
        get {return curriculum;}  
        set {curriculum = value;}  
    }
    
    /_์ดˆ๊ธฐ๊ฐ’์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ_/
    
    /_ํ•„๋“œ ๋ณ€์ˆ˜ ์„ ์–ธ_/  
    private string id;  
    private string curriculum; 
    
    public string Id  
    {  
        get {return id;}  
        set {id = "์›ํ•˜๋Š” ๊ฐ’";}  
    }  
    
    public string Curriculum
    {  
        get {return curriculum;}  
        set {curriculum = "์›ํ•˜๋Š” ๊ฐ’";}  
    }
  • ์‹ค์ œ๋กœ ์ž๋™ property๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€๊ฐ™์ด ์ƒ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค.
  • private string id; 
    
    public string GetId() 
    { 
        return id; 
    } 
    public string SetId(string value) 
    { 
        id = value; 
    } 
    
    private string curriculum;
    
    public string GetCurriculum()
    {
        return curriculum;
    }
    
    public string SetCurriculum(string value)
    {    
        curriculum = value;
    }