Encapsulate Field and Extract Interface in .NET

In this blog you will learn how to encapsulate field and extract interface using Refactor.

First of all add a new class and declare a variable email:

public class UserLog

{

    private string email;

 

    public UserLog()

          {

                   //

                   // TODO: Add constructor logic here

                   //

          }  

}

 

Now right click on email and select refactor and select Encapsulate Field.

img1.jpg 

Image 1.

 img2.jpg

Image 2.

 

And click OK.

img3.jpg 

Image 3.

 

See you class code now:

 

Private string email;

 

    Public string Email

    {

        get { return email; }

        set { email = value; }

    }

 

Now extract interface, right click on class name and click on Refactor and select Extract Interface.

 

img4.jpg 

Image 4.

 

Now provide interface name and select public menbers.

 img5.jpg

Image 5.

 

Click OK.

 

As you can see a new interface has been added in App_Code folder.

 

using System;

interface IUserLog

{

    string Email { get; set; }

}

 

As you can see your class is inheriting by this interface.

 

public class UserLog : IUserLog

{

    private string email;

 

    public string Email

    {

        get { return email; }

        set { email = value; }

    }

 

    public UserLog()

          {

                   //

                   // TODO: Add constructor logic here

                   //

          }  

}