0

When to use Methods and Properties in C#? They can do same thing but when to use both of them. And also is it possible to set a whole object via C# Property instead of single value.?

Saim Sajid
  • 19
  • 5

2 Answers2

4

A property is more or less what we use to describe different things about a class. They let us define what a class can do and essentially what that class is all about. Consider the following:

    namespace Example
    {
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public DateTime Birthday { get; set; }
        }
    }

Name, Age, and Birthday would be considered properties of the Person class. They define what a person is and give us a way to give the class value. A method would then be used to do various things with the properties. You could write a method to get or set the value of a property such as:

public string GetName()
{
    return Name;
}

public void SetName(string name)
{
    Name = name;
}

However these would be pointless considering the Name property is public meaning it can be accessed whenever we create an instance of the Person class. The above methods would be used if we wanted to set the Name property, but keep it private. Another example of a method would be if we wanted a way to say create a new instance of the person class. By default visual studio will let you instantiate a new Person object like so:

Person jim = new Person();

However we can also write our own "constructor" method to allow us to create a new Person and set it's properties at the same time.

public Person(string name, int age, DateTime birthday)
{
    Name = name;
    Age = age;
    Birthday = birthday;
}

Now we have an easy, streamlined way to instantiate a new Person object which uses a constructor method, and we can create a new Person object like so:

Person jim = new Person("Jim", 25, DateTime.Today);

But the use of methods dont stop there. Since DateTime is the way we represent the Birthday property, we could write a method that could convert a string into the appropriate DateTime.

public DateTime ConvertToDateTime(string date)
{
      DateTime temp;

      DateTime.TryParse(date, out temp);

      return temp
}

Now we can change our constructor to look like this:

public Person(string name, int age, string birthday)
{
    Name = name;
    Age = age;
    Birthday = ConvertToDateTime(birthday);
}

And can instantiate a new Person object like this:

Person jim = new  Person("Jim", 25, "1/10/1995");

On a final note, as @vivek nuna said, find a good book! A great one that I've used in previous C# classes would be Murach's book on C#. Also MSDN.com has all the documentation you would need to learn how to code in C#. Try this link to learn more about properties or this link to learn more about methods. Finally, an excellent tutorial I found to learn C# is Scott Lilly's Guide to C#. Not only will you learn the ins and outs of C#, you will get to build a pretty neat and simple text-based RPG!

Kyle Rone
  • 305
  • 1
  • 14
1

An proppertie is just a short hand and will create at the background an public get method and a public set method and a private field to store the value.

// example propertie
public string Name { get; set; }

// at run time it is the same as:
private string Name;

public string GetName(){
  return this.Name;
}

public string SetName(string name){
  this.Name = name;
}

See Image : the sample class only has an proppertie in code but if you use Reflection to get all the members off the Sample class you will see that at run time these methods are generated but not visable in code.

set_name() get_name()

'notice the private field Name is not shown because it is private and not visable for the outside, but is genrated.'

enter image description here

Timon Post
  • 2,779
  • 1
  • 17
  • 32