Named Parameters:
- This feature is introduced in C#.NET 4.0.
- This is used to pass the arguments to the method, with those names.
C# Coding on Named Parameters :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NamedParametersDemo
{
class User
{
//fields
public string FirstName;
public string LastName;
//methods
public void SetUserName(string FirstName, string LastName)
{
this.FirstName = FirstName;
this.LastName = LastName;
}
public void ShowUserName()
{
Console.WriteLine(FirstName + " " + LastName);
}
}
class Program
{
static void Main(string[] args)
{
User u = new User();
u.SetUserName(LastName: "Kumar", FirstName: "Raj"); //call a method, with
named parameters
u.ShowUserName();
Console.Read();
}
}
}
this Keyword :
- This is similar to "this pointer" in C++.
- It represents current working object.
- It is used to access the members of current working object.
- this.field
- this.property
- this.method()
- Current object : The object, with which object, the method is called.
- "this" keyword can‘t be used in the static methods, because static methods doesn‘t have current object.
C# Coding on this Keyword :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ThisKeywordDemo
{
class Sample
{
//fields
public int n = 100;
//methods
public void Increment()
{
int n = 10; //incrementation value
this.n = this.n + n;
}
}
class Program
{
static void Main(string[] args)
{
Sample s = new Sample();
Console.WriteLine(s.n);
s.Increment();
Console.WriteLine(s.n);
Console.Read();
}
}
}
Previous Articles - "ReadOnly" Fields | "Ref" & "Out" Parameter's
0 Comments