Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bodyless constructor in non-abstract C# class

I'm trying to understand the following class provided in the MVC framework; it seems like the class should be abstract, but it is not, and yet this class compiles. Is the class actually abstract despite the missing "abstract" keyword? What am I missing here?

namespace Microsoft.AspNet.Identity.EntityFramework
{
    public class IdentityUser : IUser
    {
        public IdentityUser();
        public IdentityUser(string userName);

        public virtual ICollection<IdentityUserClaim> Claims { get; }
        public virtual string Id { get; set; }
        public virtual ICollection<IdentityUserLogin> Logins { get; }
        public virtual string PasswordHash { get; set; }
        public virtual ICollection<IdentityUserRole> Roles { get; }
        public virtual string SecurityStamp { get; set; }
        public virtual string UserName { get; set; }
    }
}

Edit:

Lesson learned: if there is no source provided for a binary, using "Go To Definition" in Visual Studio will provide something that looks mostly like source code... which is different than using "Open Declaration" in Eclipse, which will show you something that really doesn't look like source.

like image 457
Mike Godin Avatar asked Oct 24 '25 04:10

Mike Godin


2 Answers

This is a Microsoft's class which I presume you retrieved the code for by "Go to Definition" context menu in Visual Studio. This code is auto generated by reflecting over the type, and only is meant to show declarations and not the implementation content. If you use .NET Refector or dotReflect tools, then you'll see body of the implementation.

like image 141
LB2 Avatar answered Oct 26 '25 10:10

LB2


What you are missing is that you are not actually looking at the source code for the class. It is part of ASP.NET, documented here. The source code is kept in Redmond Washington, out of reach of your machine and not part of the .NET Reference Source.

What you got is an auto-generated document, produced from the metadata in the Microsoft.AspNet.Identity.EntityFramework.dll assembly. Which only contains the class declaration, the code was compiled into MSIL by the C# compiler. You'd need a good decompiler to see a resemblance of that code, Reflector and ILSpy are popular.

Be careful when you use Go To Definition in Visual Studio. You only see real source code when the IDE can find it.

like image 28
Hans Passant Avatar answered Oct 26 '25 10:10

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!