C# Methods, part 1
(Page 1 of 6 )
In this article, you will learn about C# methods (functions), which form the behavior of your classes. C# does not have global functions, but it does have code encapsulation; you will learn how these and other Object Oriented Programming concepts and principles affect what you can do in C#.
C# classes carry their data in the form of fields (private fields, most of the time) and process the data using methods (functions). Methods in C# form the behavior of your class, so the class Employee may have a method called RaiseSalary that raises the salary of the employee based on his/her position in the company.
In C# there are no global functions (a C++ concept), and your methods must exist in the context of a class or struct. What's really interesting is that, when you develop a class and compile it, the other programmers who use it do not need to know anything about how you developed your methods and what happens inside each method; this called code encapsulation. So your algorithms and code are hidden (almost because they can use reverse engineering to know what's going on, but this is not the point now) from the client code.
C# is not case sensitive, so you can have a method called GetSalary() and another one called getsalary(); it doesn't make sense, but this code will compile. Please avoid doing this in your code, though, because it's a bad class design concept. Name your C# methods depending on the process they do.
For example, if you have two methods that update some data, and one of them updates a database and the other updates a file, you can name them UpdateDB() and UpdateFile() respectively. Naming your methods is an important issue in Object Oriented Programming concepts, and you should spend some time doing so in your analysis and design phase.
Next: Method Signature >>
More C# Articles
More By Michael Youssef