5

When creating a new class in .Net if I declare it "Implements IDisposable" and hit enter, I see that Visual Studio adds by itselt differents methods and functions already filled to my class. When I try to do so with my Interfaces, it creates empty methods and functions.

Is there any way to provide a default implementations of my methods and functions ?

I have been looking to Link but it didn't resolve my issue.

Example of implementation I'm looking for :

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

Cheers in advance.

Further exaplanations of what I'm looking for :

Let's assume the following Interface

enter image description here

Here is what happens and what I'm looking for :

enter image description here

Community
  • 1
  • 1
Thomas Carlton
  • 5,344
  • 10
  • 63
  • 126
  • http://msdn.microsoft.com/es-es/library/87d83y5b.aspx An interface cannot implement the functions. It only defines some functions the classes that implements the interface will have to implement Maybe you would like to check Abstract class. http://msdn.microsoft.com/en-us/library/k535acbf%28v=vs.71%29.aspx – blfuentes Oct 11 '14 at 11:52
  • 1
    Thanks for your answer. But how does .Net interfaces work ? How come they add autimatically all this code to my class when I try to implement one of them. It's this feature I'm looking for. – Thomas Carlton Oct 11 '14 at 11:57
  • 2
    It's not that clear what you're asking. – rory.ap Oct 11 '14 at 12:00
  • It knows how to set up a basic `IDisposable` implementation, as there's a common pattern you should follow. How could it possibly know about how to implement your Interfaces? – weston Oct 11 '14 at 12:01
  • That's exacly what I'm looking for Grant Winner ! – Thomas Carlton Oct 11 '14 at 12:21
  • This is hard-baked into the VB.NET IDE for IDisposable, it is not an extensible scheme. The only other way to auto-generate code is through snippets. – Hans Passant Oct 11 '14 at 12:44
  • Thanks you for your proposal. Do you know how to "link" the interface with a snippet, so that it will be inserted automatically when the user tries to implement the interface ? – Thomas Carlton Oct 11 '14 at 12:45
  • I added a picture of what i'm looking for – Thomas Carlton Oct 11 '14 at 13:15
  • Have you considered an abstract class? Not the same and only a comment. – paparazzo Oct 11 '14 at 14:11

3 Answers3

0

It knows how to set up a basic IDisposable implementation, as there's a common pattern you should follow.

But how could it possibly know about how to implement your Interfaces? So you get the empty methods which you need to fill in yourself.

Your closest bet is to set up a snippet and insert that rather than letting visual studio adding unimplemented members.

weston
  • 54,145
  • 21
  • 145
  • 203
  • Let's make it simple. When you implement IDisposable interface, not only the empty methods are added but also their codes which was certainly broght from somewhere... Look to the example I showed in my first post. The question is how to do the same thing with my own interfaces... Assuming I have a code which should be the default implementation of an interface method, when I implement a interface in a class, the code is automatically added... – Thomas Carlton Oct 11 '14 at 12:18
  • I've added another idea. – weston Oct 11 '14 at 12:32
  • Thanks you for your proposal. Do you know how to "link" the interface with a snippet, so that it will be inserted automatically when the user tries to implement the interface ? – Thomas Carlton Oct 11 '14 at 12:37
  • I don't know of any way, no. – weston Oct 11 '14 at 12:38
0

The closet thing I can think of is to create your own item template that one may select from the visual studio Add New Item dialog. You could modify the Refactoring template but that would modify all Interface implementations. Creating item templates is discussed here: http://msdn.microsoft.com/en-us/library/tsyyf0yh.aspx

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
0

This question is old, but others having same question may come here.

Above answers are outdated.

Starting with C# 8, you can have default interface methods.

The Safely update interfaces using default interface methods in C# article in the documentation explains things very well.

Following is an example taken from the documentation.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace customer_relationship
{
    public interface ICustomer
    {
        IEnumerable<IOrder> PreviousOrders { get; }

        DateTime DateJoined { get; }
        DateTime? LastOrder { get; }
        string Name { get; }
        IDictionary<DateTime, string> Reminders { get; }

        public static void SetLoyaltyThresholds(TimeSpan ago, int minimumOrders, decimal percentageDiscount) // See how a method has a default implementation.
        {
            length = ago;
            orderCount = minimumOrders;
            discountPercent = percentageDiscount;
        }
        private static TimeSpan length = new TimeSpan(365 * 2, 0, 0, 0); // two years
        private static int orderCount = 10;
        private static decimal discountPercent = 0.10m;

        public decimal ComputeLoyaltyDiscount() => DefaultLoyaltyDiscount(this);
        protected static decimal DefaultLoyaltyDiscount(ICustomer c)
        {
            DateTime start = DateTime.Now - length;

            if ((c.DateJoined < start) && (c.PreviousOrders.Count() > orderCount))
            {
                return discountPercent;
            }
            return 0;
        }
    }
}
Youssef13
  • 3,836
  • 3
  • 24
  • 41