Saim Sajid Saim Sajid - 20 days ago 7
C# Question

C# Methods and Properties

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.?

Answer

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# (link redacted because I can't post more than 2). Not only will you learn the ins and outs of C#, you will get to build a pretty neat and simple text-based RPG!