+ 5
Why we are using classes can't we do the same thing with methods?
Like creating an object for class bankAccount can't we create a method and call that method. Please help me to clear my doubt.
5 Answers
+ 19
Classes and Methods are different things, don't be mistaken on what their purpose are.
Methods are placed inside Classes.
Methods can't exist by themselves, they have to be placed somewhere, there must be a file that holds them.
Methods perform specific tasks while Classes are the files that hold methods.
example:
Person is a class.
inside the Person class is the SetName() and GetName() methods that assigns and retrieves the name of the Person instance.
Person me = new Person();
me.SetName("Erwin");
Console.Write(me.GetName());
//It will print Erwin
Can you see the difference now?
+ 5
bro methods are collection of statements...
and classes are collection of methods :)
+ 5
To add to Erwin's response, it's also part of separation of concerns in your code and code reusability. technically you could sort of write an entire program in a main method but then you'd have a large ugly mess of unreadable code.
+ 2
In the C# language it is not possible to have functions that are not a member of a class like in a functional language. C# (.NET) is fully OO (Object Oriented).
Think of classes as "things" with responsibillities. These responsibillities are implemented by using data and functions that act on this data. So data and functions are closely related and "live" together in a class.
+ 2
thank you all for the reply, it was clarifying