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

23rd Aug 2016, 5:29 PM
Amit
5 odpowiedzi
+ 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?
23rd Aug 2016, 5:53 PM
Erwin Mesias
Erwin Mesias - avatar
+ 5
bro methods are collection of statements... and classes are collection of methods :)
31st Aug 2016, 2:07 PM
Ammar Ahmad
Ammar Ahmad - avatar
+ 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.
3rd Sep 2016, 3:14 AM
Mike
Mike - avatar
+ 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.
23rd Aug 2016, 10:14 PM
Marco Roessen
Marco Roessen - avatar
+ 2
thank you all for the reply, it was clarifying
7th Sep 2016, 9:36 PM
Augusto Giuliano
Augusto Giuliano - avatar