Skip to content

Commit

Permalink
Create Extension Method.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
BhaskarBVV authored Oct 7, 2022
1 parent 14fa46f commit 1a7d0f4
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Extension Method.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Xml.Linq;

namespace ext
{
public sealed class user
{
public int id { get; set; }
public string name { get; set; }
public void print()
{
Console.WriteLine($"{id} {name}");
}

}

//Extension class and its functions must be static.
//Extension class function cannot override the base class function,
//the extension function can be called in case the signature is different.
//With extension method we don't need to subclass the base class,
public static class AddFuncToUser
{
public static void print(this user u)
{
Console.WriteLine("Override");
}
public static string userData(this user u)
{
string user_rec = "";
user_rec += Convert.ToString(u.id);
return user_rec;
}
// Extension method cannot access the priavte/ protected fields.
// we cannot add property as a extension method.
// also cannot add a new variable/ field, but both can be done by inheritance.
//public string pro1
//{
// property
// get { return name; }
// set { name = value; }
//}
}

public class main
{
public static void Main()
{
user u = new user();
u.name = "Bhaskar";
u.id = 1;
u.print();
}
}
}

0 comments on commit 1a7d0f4

Please sign in to comment.