Showing posts with label Ref. Show all posts
Showing posts with label Ref. Show all posts

Thursday, August 25, 2011

Partial Methods in C#


All of us know that partial type allows us to split definition of type across multiple files but do you know C# also provides supports for partial methods too. Partial methods are used when your code is auto generated by code generation tool. The tool will write only method definition and leave the implementation of for developer if they want to customize.

Basically partial method contains two parts, method definition and implementation. The definition is mostly written by code generation tool and implementation is written manually by developer. If no implementation provided then compiler removes method signature at compile time.

Below things should be kept in mind while creating partial method

  • Partial methods must be declared inside a partial class.
  • Partial methods can have ‘ref’ parameters but not ‘out’ parameters.
  • Partial methods are implicitly private therefore they can’t be Virtual.
  • Partial methods must be void.

Below example demonstrate use of partial methods.

public partial class MyClass
{
    partial void MyMethod();
}
public partial class MyClass
{
    public MyClass()
    {
        MyMethod();
    }
    partial void MyMethod()
    {
        Console.WriteLine("Hello");
    }
}


Hope you like this Tip. 


Thanks for reading. Take Care.


See also
How to use Keyword as an identifier
How to check Arithmetic Operation Overflow

Monday, June 13, 2011

Named and Optional Arguments – What’s new in C# 4.0



Microsoft introduced new feature called Named and optional arguments in C# 4.0. This feature is already available in Visual Basic. This feature allows user to enter optional and named arguments with method parameters. These are two different features but mostly used together.

Optional parameter is declared by simply declaring default value for it. In below example parameter “s” and “b” are optional parameters. If you don’t specify any arguments for optional parameters it will use default values. You can specify optional parameters for Method, Constructor and indexer.  

private void Test(int a, string s= "Test", bool b= false)
{
//code
}

Test(45, "Mitesh", true); // Valid
Test(33); // Valid
Test(44, false); // Invalid

C# doesn’t allow you to omit arguments between commas instead you can specify name with argument. You don’t need to remember order of parameters you simply specify argument with name of parameter.  

Test(44 ,, false); // Invalid, Comma separated gapes are not allowed

Instead,

Test(44 ,b:false); // Valid
Test(36, b:true, s:"MyName"); // Valid
Test(b:true, 56); // Invalid, Named arguments must be specified after all fixed arguments

Restrictions:
  • Optional parameters must be added only after all required parameters.
  • Named arguments must be specified after all fixed arguments.

    Note: Ref keyword is also optional when you using COM and passing object as reference.