TEP , The Engineering Projects , Author Welcome! Syed Zain Nasir Mechatronics Engineer TEP , The ENgineering Projects , TEP Badge TEP , The Engineering Projects , Ratting Background 7.5 7.5/10
TEP , The Engineering Projects , Rating Percentage Background Reputation Score 55 / 100
Introduction to Methods in C#, methods in c#, methods c#, c# methods, static methos in c#, instance method c#
TEP , The Engineering Projects , Boxes

Introduction to Methods in C#

TEP , THe Engineering Projects , Shares 2.5K Views
TEP , The ENgineering Projects , Reaction 40
TEP , The ENgineering Projects , Reaction 700
TEP , The ENgineering Projects , Reaction 60
TEP , The ENgineering Projects , Reaction 25
TEP , The ENgineering Projects , Reaction 60
TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest
Shares: 691
TEP , The Engineering Projects , PCBWAY
Introduction to Methods in C#, methods in c#, methods c#, c# methods, static methos in c#, instance method c#
Hello friends, I hope you all are doing great. In today tutorial, I am going to give you a detailed Introduction to Methods in C#. It's our 12th tutorial in C# series. So far, we have covered all the basic concepts in C# and now it's time to move forward and have a look at some complex concepts. Methods have an important role in C# programming and if you want to be an efficient programmer then you must set your method controls correctly. Some methods have secret codes in them, which you don't want to give access to your developers, then you can set it private. We will cover such things in detail later, let's first have a look at Introduction to Methods in C#:

Introduction to Methods in C#

  • Methods in C#, also called Functions, are extremely useful in optimizing the code, normally used to create a logic once and use it repeatedly.
  • It happens in projects where you need to do a similar job at various places of your code.
  • In such conditions, it's wise to create a small function of your repeated code and instead of adding those lines again and again, simply call the function.
  • Here's the syntax of Methods in C#:
Access-Modifiers Return-Type Method-Name ( Parameters )  { // Method-Body } public void HelloWorld(){ Console.Write("Hello World !!!"); }
  • Access-Modifiers: Access Modifiers are used to control access capability of any method / function. You can set them public, protected or private etc. We will discuss them later in detail.
  • Return-Type: It decides what the method is gonna return, it could be void or any data type i.e. int, float, string etc.
  • Method-Name: It's the unique name of the Method / Function, which can't be any reserved keywords in C#. It's should be meaningful so you can remember later.
  • Parameters: Parameters are optional and are normally used to transfer data between methods.
  • There are two types of methods available in C#, which are:
    • Instance Method.
    • Static Method.
  • Let's discuss both of them separately, in detail:

Instance Method in C#

  • Those methods, which doesn't have static keyword in their definition are called Instance Methods in C#.
  • In order to call instance method in another method, we have to first create a new instance of that method and the invoke the method using dot operator.
  • Let's have a look at how to create and call an instance method in C#:
Introduction to Methods in C#, methods in c#, methods c#, c# methods, static methos in c#, instance method c#
  • You must have realized by now that all the work we have been doing so far was in Main Method. That's the default method, the C# compiler first goes into and it's a static method as it has static keyword in it, which will discuss next.
  • So, now in above figure, I have created a new method and I have used public (access-modifier) and void (return-type) and the Method-Name is PrintNames. I am using studentsNames array as a parameter.
  • I have used the same code which we have designed in our previous lecture on For Loop in C#, so I have placed the foreach loop in my newly created instance method named PrintNames.
  • Now in order to call this new method in Main method, I have to first create a new instance of this method's class and you must have noticed that the class name is Program at the top.
  • Both of our Main & PrintNames Methods are placed inside Program class.
  • So, in our Main function, I have first created a new instance of my class Program using new keyword:

Program P = new Program ( );

  • After that, using this class instance P, I have invoked the PrintNames method using dot operator ( . ).

P.PrintNames ( studentsNames ) ;

  • When you are calling your method, make sure you enter the parameters correctly as specified in its definition otherwise compiler will generate an error.
  • You can specify multiple parameters separated by commas.
  • So, that's how we can create and call an instance method, so now let's have a look at How to call static method in C#.

Static Method in C#

  • Those methods, which have static keyword in its definition are called static methods in C#.
  • In order to call a static method, we don't need to create a new instance of Program class.
  • Instead, we can directly invoke a static method from Program class using dot operator ( . ), as shown in below figure:
Introduction to Methods in C#, methods in c#, methods c#, c# methods, static methos in c#, instance method c#
  • In above figure, I have created a new method called PrintNames2 and I have used static keyword in its definition, so its a static method in C#.
  • So, now in order to call that method, I have used the name of class and then invoked the method using dot operator, using below code:

Program.PrintNames2 ( stringNames );

  • We don't need to create a new instance of class for static method, that's the difference between static and instance methods in C#.
So, that was all about Methods in C# and I hope you have understood the difference between static and instance methods and how to invoke them. In the next lecture, we will have a look at Introduction to Namespace in C#, which is another important concept in C#. Till then take care & have fun !!! :)
TEP , The Engineering Projects , Tags
Introduction to Methods in C#
methods in c#
methods c#
c# methods
static methos in c#
instance method c#
TEP , The Engineering Projects , Calender Monday, November 18, 2019
TEP , The Engineering Projects , Comments

Write a Comment

TEP , The Engineering Projects , WordPress TEP , The Engineering Projects , Google TEP , The Engineering Projects , Twitter TEP , The Engineering Projects , Facebook TEP , The Engineering Projects , User
TEP , The Engineering Projects , Robot
TEP , The Engineering Projects , Comments Comments on ‘’ Introduction to Methods in C# ‘’ (0)
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Top PCB Design Service

PCBA
TEP , The Engineering Projects , JLCPCB
PCB
TEP , The Engineering Projects , JLCPCB
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Embedded Tools

ARDINO TEP , The Engineering Projects , JLCPCB
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Subscribe Now !!!

Learn Free Pro Tricks

TEP , The Engineering Projects , Mail Receive Quality Tutorials Straight in your Inbox by Submitting your Email ID Below
TEP , The Engineering Projects , Mail TEP , The Engineering Projects , Mail Shadow
TEP , The Engineering Projects , Mail Robot TEP , The Engineering Projects , Mail Robot Shadow TEP , The Engineering Projects , Mail Robot Shadow
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Engineering Books

TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for arduino $20
TEP , The Engineering Projects , Rating Stars (5.0)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for Raspberry Pi $20
TEP , The Engineering Projects , Rating Stars (3.0)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for arduino $20
TEP , The Engineering Projects , Rating Stars (4.7)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for Raspberry Pi $20
TEP , The Engineering Projects , Rating Stars (5.0)
TEP , The Engineering Projects , TEP Robot TEP , The Engineering Projects , TEP Robot Shadow TEP , The Engineering Projects , TEP Robot Shadow
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Categories

TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K