C# METHODS
§ A Method is a block of code that executes when its invoked.
§ A Method is also known as functions.
§ A Method is very useful concept to repeat a same code many times in our programmes.
§ In C# Methods we can use methods with passing a parameter or without passing a parameter as per our requirements and needs.
§ Every C# programmes have at least one method with named Main.
§ Here is a small diagram to understand about Method.
§ As we can see in above diagram of Method many things are there to be understand, we can take one by one things with some explanations.
o Public – public keyword is very often used in C# programmes. public keyword is an access modifier.
o It is accessible for all the classes.
o Int – Int refers for return type ,if we put Int keyword in our method it is mandatory to use return keyword in its body otherwise it will throw an error cause Int, string, float etc are that keyword which have return types, one keyword which we are seeing many C# programmes is void which has no return type.
o Max – Max keyword refers to method name, we can use any name for our method as per our choice but there is some rules to choose a name in C# Programmes. Here rules are as follows...
§ Names can contain letter, digit and underscore(_)
§ Names should start with a letter and underscore(_) not with a number.
§ Between two words or two letters there should not be a white spaces.
§ Names are case sensitive for eg (my Var and My var )both are different.
§ Reserved keywords of C# cannot used for names for eg (Int ,double )etc.
o (Int x) - It refers to parameter list , Parameters may empty also and contain some variables also, we can use as per our needs and requirements. If we used return type in method we have to put that type of variable in parameter for eg if we use return type Int then we can create only parameter lists of Int type not of other types or we can also be left blank.
o Body of the method – Here we can write our code that we have to call inside curly braces.
o Its all about body of the method.
o In C# there are two types of methods they are as follows:,
o Static Method
o Instance Method
o Instance Method – Instance method is a method that have no static keyword and it work with object of a class where the object named followed by a period(.)and the name of the method followed by the paranthesis.
o Static Method – Static method is a method that have static keyword and it work with a classes not an instances of classes.
C# PARAMETERS
o Information can be passed to methods as parameters,Inside the method parameters behave as a local variable.
o Parameters are used after the method name, We can pass many parameters in a single method with a separation of comma for eg (int x,int,y,int z) this way we can do this.
C# METHOD OVERLOADING
o In C# Method overloading refers to we can use many methods in a single class with same name same but with different parameters.
o Instead of defining two methods that should be the same thing it is better to overload one.
o In Summary of Methods we cover many things that what is methods and whats is uses then we have seen the structure of method we discuss about its parameter and last we have seen Mehtods overloading.
o Method is very useful for reuse one code many times in our programmes. for this we safe to write same code every time this will make our programme lengthy also as well as our compiler also get overload to execute it . Mehtods providing convenience and good readability to our programmes in C#
Take the first step towards data-led growth by partnering with MSA Infotech. Whether you seek tailored solutions or expert consultation, we are here to help you harness the power of data for your business. Contact us today and let’s embark on this transformative data adventure together. Get a free consultation today!
We utilize data to transform ourselves, our clients, and the world.
Partnership with leading data platforms and certified talents