C_Sharp_Language

C Sharp TutorialC Sharp .NET Compiler Platform (Roslyn)C Sharp 3.0 FeaturesC Sharp 4.0 FeaturesC Sharp 5.0 FeaturesC Sharp 6.0 FeaturesC Sharp 7.0 FeaturesC Sharp Access ModifiersC Sharp Access network shared folder with username and passwordC Sharp Accessing DatabasesC Sharp Action FiltersC Sharp Aliases of built-in typesC Sharp an overview of collectionsC Sharp Anonymous typesC Sharp ArraysC Sharp ASP.NET IdentityC Sharp AssemblyInfo.cs ExamplesC Sharp Async-AwaitC Sharp Async/await, Backgroundworker, Task and Thread ExamplesC Sharp Asynchronous SocketC Sharp AttributesC Sharp Authentication handlerC Sharp BackgroundWorkerC Sharp BigIntegerC Sharp Binary SerializationC Sharp BindingListC Sharp Built-in TypesC Sharp CachingC Sharp CastingC Sharp Checked and UncheckedC Sharp CLSCompliantAttributeC Sharp Code ContractsC Sharp Code Contracts and AssertionsC Sharp Collection InitializersC Sharp Comments and regionsC Sharp Common String OperationsC Sharp Conditional StatementsC Sharp Constructors and FinalizersC Sharp Creating Own MessageBox in Windows Form ApplicationC Sharp Creational Design PatternsC Sharp Cryptography (System.Security.Cryptography)C Sharp Data AnnotationC Sharp DateTime MethodsC Sharp DelegatesC Sharp Dependency InjectionC Sharp DiagnosticsC Sharp Dynamic typeC Sharp EnumC Sharp Equality OperatorC Sharp Equals and GetHashCodeC Sharp EventsC Sharp Exception HandlingC Sharp Expression TreesC Sharp Extension MethodsC Sharp File and Stream I/OC Sharp FileSystemWatcherC Sharp Func delegatesC Sharp Function with multiple return valuesC Sharp Functional ProgrammingC Sharp Garbage Collector in .NETC Sharp Generating Random NumbersC Sharp Generic Lambda Query BuilderC Sharp GenericsC Sharp Getting Started: Json with C SharpC Sharp GuidC Sharp Handling FormatException when converting string to other typesC Sharp Hash FunctionsC Sharp ICloneableC Sharp IComparableC Sharp IDisposable interfaceC Sharp IEnumerableC Sharp ILGeneratorC Sharp ImmutabilityC Sharp Implementing Decorator Design PatternC Sharp Implementing Flyweight Design PatternC Sharp Import Google ContactsC Sharp Including Font ResourcesC Sharp IndexerC Sharp InheritanceC Sharp Initializing PropertiesC Sharp INotifyPropertyChanged interfaceC Sharp InterfacesC Sharp InteroperabilityC Sharp IQueryable interfaceC Sharp IteratorsC Sharp KeywordsC Sharp Lambda expressionsC Sharp Lambda ExpressionsC Sharp LINQ QueriesC Sharp LINQ to Objects



C Sharp Methods

From WikiOD

Calling a Method[edit | edit source]

Calling a static method:

// Single argument
System.Console.WriteLine("Hello World");  

// Multiple arguments
string name = "User";
System.Console.WriteLine("Hello, {0}!", name);

Calling a static method and storing its return value:

string input = System.Console.ReadLine();

Calling an instance method:

int x = 42;
// The instance method called here is Int32.ToString()
string xAsString = x.ToString();

Calling a generic method

// Assuming a method 'T[] CreateArray<T>(int size)'
DateTime[] dates = CreateArray<DateTime>(8);

Anonymous method[edit | edit source]

Anonymous methods provide a technique to pass a code block as a delegate parameter. They are methods with a body, but no name.

delegate int IntOp(int lhs, int rhs);
class Program
{
    static void Main(string[] args)
    {
        // C# 2.0 definition
        IntOp add = delegate(int lhs, int rhs)
        {
            return lhs + rhs;
        };

        // C# 3.0 definition
        IntOp mul = (lhs, rhs) =>
        {
            return lhs * rhs;
        };

        // C# 3.0 definition - shorthand
        IntOp sub = (lhs, rhs) => lhs - rhs;

        // Calling each method
        Console.WriteLine("2 + 3 = " + add(2, 3));
        Console.WriteLine("2 * 3 = " + mul(2, 3));
        Console.WriteLine("2 - 3 = " + sub(2, 3));
    }
}

Parameters and Arguments[edit | edit source]

A method can declare any number of parameters (in this example, i, s and o are the parameters):

static void DoSomething(int i, string s, object o) {
    Console.WriteLine(String.Format("i={0}, s={1}, o={2}", i, s, o));
}

Parameters can be used to pass values into a method, so that the method can work with them. This can be every kind of work like printing the values, or making modifications to the object referenced by a parameter, or storing the values.

When you call the method, you need to pass an actual value for every parameter. At this point, the values that you actually pass to the method call are called Arguments:

DoSomething(x, "hello", new object());

Return Types[edit | edit source]

A method can return either nothing (void), or a value of a specified type:

// If you don't want to return a value, use void as return type.
static void ReturnsNothing() { 
    Console.WriteLine("Returns nothing");
}

// If you want to return a value, you need to specify its type.
static string ReturnsHelloWorld() {
    return "Hello World";
}

If your method specifies a return value, the method must return a value. You do this using the return statement. Once a return statement has been reached, it returns the specified value and any code after it will not be run anymore (exceptions are finally blocks, which will still be executed before the method returns).

If your method returns nothing (void), you can still use the return statement without a value if you want to return from the method immediately. At the end of such a method, a return statement would be unnecessary though.

Examples of valid return statements:

return; 
return 0; 
return x * 2;
return Console.ReadLine();

Throwing an exception can end method execution without returning a value. Also, there are iterator blocks, where return values are generated by using the yield keyword, but those are special cases that will not be explained at this point.

Default Parameters[edit | edit source]

You can use default parameters if you want to provide the option to leave out parameters:

static void SaySomething(string what = "ehh") {
    Console.WriteLine(what);
}  

static void Main() {
    // prints "hello"
    SaySomething("hello"); 
    // prints "ehh"
    SaySomething(); // The compiler compiles this as if we had typed SaySomething("ehh")
}

When you call such a method and omit a parameter for which a default value is provided, the compiler inserts that default value for you.

Keep in mind that parameters with default values need to be written after parameters without default values.

static void SaySomething(string say, string what = "ehh") {
        //Correct
        Console.WriteLine(say + what);
    }

static void SaySomethingElse(string what = "ehh", string say) {
        //Incorrect
        Console.WriteLine(say + what);
    }

WARNING: Because it works that way, default values can be problematic in some cases. If you change the default value of a method parameter and don't recompile all callers of that method, those callers will still use the default value that was present when they were compiled, possibly causing inconsistencies.

Method overloading[edit | edit source]

Definition : When multiple methods with the same name are declared with different parameters, it is referred to as method overloading. Method overloading typically represents functions that are identical in their purpose but are written to accept different data types as their parameters.

Factors affecting

  • Number of Arguments
  • Type of arguments
  • Return Type**

Consider a method named Area that will perform calculation functions, which will accepts various arguments and return the result.

Example

public string Area(int value1)
{
    return String.Format("Area of Square is {0}", value1 * value1);
}

This method will accepts one argument and return a string, if we call the method with an integer(say 5) the output will be "Area of Square is 25".

public  double Area(double value1, double value2)
{
    return value1 * value2;
}

Similarly if we pass two double values to this method the output will be the product of the two values and are of type double. This can be used of multiplication as well as finding the Area of rectangles

public double Area(double value1)
{
    return 3.14 * Math.Pow(value1,2);
}

This can be used specially for finding the area of circle, which will accepts a double value(radius) and return another double value as its Area.

Each of these methods can be called normally without conflict - the compiler will examine the parameters of each method call to determine which version of Area needs to be used.

string squareArea = Area(2);
double rectangleArea = Area(32.0, 17.5);
double circleArea = Area(5.0); // all of these are valid and will compile.

    • Note that return type alone cannot differentiate between two methods. For instance, if we had two definitions for Area that had the same parameters, like so:
public string Area(double width, double height) { ... }
public double Area(double width, double height) { ... }
// This will NOT compile.

If we need to have our class use the same method names that return different values, we can remove the issues of ambiguity by implementing an interface and explicitly defining its usage.

public interface IAreaCalculatorString {

    public string Area(double width, double height);

}

public class AreaCalculator : IAreaCalculatorString {

    public string IAreaCalculatorString.Area(double width, double height) { ... } 
    // Note that the method call now explicitly says it will be used when called through
    // the IAreaCalculatorString interface, allowing us to resolve the ambiguity.
    public double Area(double width, double height) { ... }

Declaring a Method[edit | edit source]

Every method has a unique signature consisting of a accessor (public, private, ...) ,optional modifier (abstract), a name and if needed method parameters. Note, that the return type is not part of the signature. A method prototype looks like the following:

AccessModifier OptionalModifier ReturnType MethodName(InputParameters)
{
    //Method body
}

AccessModifier can be public, protected, pirvate or by default internal.

OptionalModifier can be static abstract virtual override new or sealed.

ReturnType can be void for no return or can be any type from the basic ones, as int to complex classes.

a Method may have some or no input parameters. to set parameters for a method, you should declare each one like normal variable declarations (like int a), and for more than one parameter you should use comma between them (like int a, int b).

Parameters may have default values. for this you should set a value for the parameter (like int a = 0). if a parameter has a default value, setting the input value is optional.

The following method example returns the sum of two integers:

private int Sum(int a, int b)
{
    return a + b;
}

Access rights[edit | edit source]

// static: is callable on a class even when no instance of the class has been created
public static void MyMethod()

// virtual: can be called or overridden in an inherited class
public virtual  void MyMethod()

// internal: access is limited within the current assembly
internal  void MyMethod()

//private: access is limited only within the same class
private  void MyMethod()

//public: access right from every class / assembly
public void MyMethod()

//protected: access is limited to the containing class or types derived from it
protected void MyMethod()

//protected internal: access is limited to the current assembly or types derived from the containing class.
protected internal void MyMethod()

Credit:Stack_Overflow_Documentation