What is a “method” in C# programming?

750 views

I am currently going through “Become a C# Developer” on LinkedIn Learning and watching this course “Understanding C# syntax”, the instructor – Bruce Van Horn – went on to explain properties but he never explained what is a method? I mean, what is a method? What is it used for? How do I know where to look for method?

Is this simply just a word – like in real world – a method to do something? E.g. completing a task using method 1 or method 2?

TL;DR: what is a method? What is it used for? How do I know where to look for method?

Also, I have Googled this using keyword, but no real good explanations come out of it that I understand.

In: Engineering

6 Answers

Anonymous 0 Comments

A property is just a fundamental datatype or a construct of said datatypes that are attached as an attribute of the object. You can view it, you can modify it, but it just kinda sits there. It doesn’t DO anything.

A method on the other hand is a function – it takes inputs, does stuff (possibly to or with the object’s own properties) and returns outputs. Maybe other objects, or the values of the object’s properties.

Objects usually have some special methods. One is a constructor, and one is a destructor – these are special methods called when an instance of that object is created, or destroyed/deleted/freed respectively.

If you as a person were an object, your properties would be “hair color” or “eye color”. Your methods would be “eat food” or “run”.

Anonymous 0 Comments

Methods are the verbs of your coding language – they make the software do something. Generally speaking, in object-oriented languages, the objects are your nouns, properties are adjectives and the methods are the verbs. When you are defining an object, you say what they can do with methods.

Anonymous 0 Comments

Methods are technically different from functions, but in practice the two terms are identical.

Another answer has explained what a method is, so I want to explain in a bit more detail what properties are in C# because it’s not fully intuitive.

A ‘field’ is a variable attached to an object. So, if you have a Person class and you say that class has an Age, represented by an int variable, that’s a field.

In programming we often want to control how we access fields. This is why we use ‘public’ and ‘private’ — they let us decide who gets to see the inner workings of a class. You could declare the Person class’ Age field as private (nobody outside Person can see it) or public (everyone can see it).

A property is a more detailed and powerful way to determine the visibility of fields. In practical terms, they’re methods — nothing less and nothing more. You have a private field, and then public methods which control how other parts of your program see or set the value of the private field. An example:

class Person

{

private int age;

public GetAge() { return age; }

public SetAge() { age = value; }

}

You could, of course, also use that SetAge() function to do ‘validation’ — making sure nobody sets the variable to something stupid or nonsensical, like a negative number or 20,000.

Anonymous 0 Comments

It’s a function

They’re called methods in Java

They’re called functions in C

C# is basically Microsoft’s equivalent to Java so that’s why he may call it a method

Anonymous 0 Comments

All the other answers are correct, but they lack the specific information that a method is a *member function of a class*. Other programming languages have things called functions that definitely aren’t methods, for example in C++, int main(int argc, char* argv[]){ return 0;} is a function, but it’s not a method, because there’s no class associated with it. On the other hand, in C# everything is defined within a class, so you cannot define a function that is not a method.

Anonymous 0 Comments

I don’t know C# but I do know. However, I am familiar with Object Orientated Programing.

A property holds key value pairs. Such as `name: “Bob”`

That is a property, it holds a value.

However, if the key is a function (a set of instructions to reuse) then it’s a method. such as `Person.sayHello()`

would print “hello bob”, if I coded the function to that of course.