Extension methods are a great way to extend the functionality of a type.
Are there any ways similar to this which can be used to extend properties of a class without inheriting a new class.
No extension properties do not exist.
You can't do it via properties without inheriting a new class. There are only extension methods, not extension properties (it may be added at a future date). If you don't want to alter the original class you should inherit from the original class and then add your properties to the derived class.
Related
Nearly in all of the "Abstraction" principle definitions, it says sth. like "Hiding the irrelevant (or extra, not needed to know) codes from the user". And I could not get how this is about "abstraction".
Let's say "This is because we can define the methods in the base abstract classes, and we can use these methods after we created the objects in child classes without knowing the internal functionality" But we could already have base non-abstract classes for this purpose. We could have already created objects and used the functionality of the base class without knowing the internal structure of those methods. How is this feature belongs to "abstraction" ????
The main advantage of creating an abstract base class over a non-abstract base class is, that it can not be instantiated; so it is just be there for creating a "common concept", right? But other than that, i don't see a difference between them in the sense of hiding the internal structure.
Let's say I have 2 types of coffee-machine; CheapMachine and ExpensiveMachine. I created a non-abstract base class and added several methods with definitions like StartMachine(), GetOptimumHeat(), etc. And then I created a CheapMachine instance CP, and called these methods easily, without knowing the internal functionality of these methods.
So, did I use abstraction principle by just creating a base class then?
I am confused like I feel I got the main idea but what would be the best description of this principle? For ex., How would you describe this principle in an interview?
I have a base class which takes in constructor arguments. I've added another one in, but now I have to change the base class in the derived classes.
Is there anyway in Resharper I can add this dependency into derived classes easily? I have about 1000 derived classes and wondering how to do this easily.
Thanks
I have reverse engineered a MySQL database in a C# desktop app. What I would like to do is extend one of the model classes, so that I can add methods to it to use locally in my application. I don't want to change any properties or anything just. Just get information and calculate things.
The problem is that when I inherit from one of the model classes I get an error about a new discriminator field being in the class but not the database.
Is there a way to do what I want to do?
Given that the model classes are partial, you can just declare your own partial classes to join them:
// Note - needs to be in the same namespace as the auto-generated declaration
public partial class Foo
{
// Add your own methods here, which can refer to the properties declared
// for the same type in the auto-generated code
}
The point of partial classes is that multiple files can contribute source to the same type.
You could try extension methods to accomplish this instead of inheriting and creating a new subtype.
You would "attach" the extension methods to the model class which you generated.
In C#...
I'm designing a base class (can NOT use constructors for this), and I want to force any inheriting classes to implement a method to initialize their respective objects. I can expect that their initialization method may take an arbitrary number and type of parameters.
how would I declare this abstract method in the base, so that the children can implement any parametered variation of it? If it is not possible, could you suggest an alternative? Thank you.
updated details : the base class should be abstract, i need a method to act as a constructor (but I can't use a constructor to do it). The child class needs a way to initialize itself, and I was hoping to force an implementation with an (abstract?) method of my own
I think it is possible try this code in your base class
public abstract void Test(params object[] list);
now it will force to declare method in child class.
Hope this help.
In addition, you can use new Modifier to hide explicitly the same name methods, fields, ... of derived class
.NET framework 4.0 provide optional parameters and name arguments to deal with a various type of parameters Named and Optional Arguements
We have an abstract class where all properties have private setters. In our concrete derived class, the code generator is creating a static “create” method that attempts to set the properties of the abstract class. Obviously this fails since the setters are private.
How do we suppress the creation of the “create” method?
In v3.5 I don't think you can suppress just this part. However, it will only attempt to set non-nullable/required properties. So I see a few options. None are ideal.
Hang on for v4.0, where you can customize codegen.
Abandon EF codegen altogether and use a custom data class.
Make the properties nullable.
Don't put the properties on the parent type. Put them on the subtypes and use an interface for polymorphism.
Don't make the properties private.