TagMethod

MethodInfo

Type’s class GetMethods method returns us with a array of MethodInfo objects. Similarly GetMethod method provided with a name of Method returns us a  MethodInfo object. It allows us to access a lot of information and interaction concerning specific Method, such as:

Check if a method is a constructor,

Access modifier of a method,

Method parameters,

Method name,

Invoking/Calling method

And many more…

Example:

We will continue our voyage with Reflection in next lesson about Assembly class.

 

Properties

It is good practice to keep our fields private but so far we had to create at least two methods to access or change their values.

 

Why not star using properties? You consume properties as fields and write them (almost) as methods.

Simple property example:

 

But why stop there we can add more functionality to our properties, like set our string name to Upercase, return default value or check if integer is a positive number.

 

A property that has both accessors is read-write property. Properties with only get accessor are called read-only properties, properties with only get accessor are called write-only properties.

 

When you only need basic property without any additional logic, you could create auto-implemented property without filed (actually compiler creates one for you).

Simple isn’t it 🙂

Methods

Methods are used to for plenty of reasons:

  • Cleaner code
  • Code re-usability
  • Easier maintenance
  • Code readability

 

How does method actually look like?

Method consists of method signature and method body.

 

Method signature signature always look like this:

  • Access modifier
  • Return type or void
  • Method name
  • Parameters

 

 

And After method signature we have

  • Opening curly brackets
  • Method body/our logic
  • Closing curly brackets

 

 

Let’s dissect this method:

 

Our method could be void, meaning that method will not return any value.

 

Of course, our method can also be parameter-less, contain multiple parameters or with different access modifiers. We could also have multiple methods with same name but will have to have different parameters or at-least different order of same parameters.

 

© 2021 LearnC#WithDarko

Theme by Anders NorenUp ↑