Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If a class instantiated it will create an object.Memory will allocated to the instance. What happens if Interface intstantiated? Does interface has constructors? Does it create an interface object.does it alllocate memory to interface object
interface IInteface {}
class Test : IInterface{}
IInterface ex1 = new Test();
what above line will create ?
Interfaces are abstract concepts that cannot be instantiated. They serve to define a contract for implementing classes to fulfill.
Then, you can create instances of concrete classes implementing the interface (usually with new), and use an interface reference to point to that instance.
Interfaces have no constructors and can't be created by themselves.
Assigning an object to variable (including variable of interface type) does not create new object, it is just another reference to the same object.
class DerivedWithInterface: Base, IEnumerable {}
Now you can create instances of DerivedWithInterface class and assign to variable of any of base classes/interfaces, but only new will create an object:
DerivedWithInterface item = new DerivedWithInterface();
IEnumerable asEnumerable = item; // asEnumerable is the same object as create before
Base asBase = item;
Now you can do casts back to original object and there still will be exactly one (or as many as you've newed up):
IEnumerable asEnumerableItem = new DerivedWithInterface();
DerivedWithInterface itemViaCast = (DerivedWithInterface)asEnumerableItem;
both asEnumerableItem and itemViaCast refer to the same single instance of and object of type asEnumerableItem
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I've tried to search for other questions like mine but I can't find any good result
I'm trying to use a DLL that has a method that need to be called with a new class but that class is abstract.
thanks.
i tried to use new IClientHandler but it just errors
You're trying to instantiate an abstract class in C# code. This is not possible, as an abstract class is a class that cannot be instantiated on its own. Instead, you would need to create a non-abstract class that extends the abstract class and then use that to create an object.
Here's an example:
abstract class AbstractClass
{
public abstract void AbstractMethod();
}
class ConcreteClass : AbstractClass
{
public override void AbstractMethod()
{
Console.WriteLine("I'm a non-abstract method!");
}
}
// Now we can create an instance of ConcreteClass
var instance = new ConcreteClass();
You can then call the AbstractMethod on the instance object, since it has been implemented in the ConcreteClass.
Also my small mistake, as #ewerspej noticed:
IClientHandler is an interface, not an abstract class. Neither can be instantiated. Interfaces require implementations that can be instantiated and abstract classes must be extended by non-abstract classes that can be instantiated.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I don't know whether this question will make sense or if it's a naive question, but I haven't been able to find a satisfactory answer.
I would like to be able to share code across multiple static classes. Is there an "equivalent" of an abstract base class for static classes (i.e. can I have one static class inherit from another one)? If not, why not? Is there another way of sharing code across multiple static classes in a manner analogous to what you do with an abstract base class?
Also, how does a static class compare to a "sealed" class?
You're quite right. Reflection will show that a static class is both abstract and sealed:
public static class MyStaticTest {
}
...
// I'm abstract
Console.WriteLine(typeof(MyStaticTest).IsAbstract ? "I'm abstract" : "");
and that's why you can't create an instance (compile time error): new MyStaticTest();. However, reflection will show that any static class is sealed as well:
// I'm sealed
Console.WriteLine(typeof(MyStaticTest).IsSealed ? "I'm sealed" : "");
and so you can't inherit from it (compile time error): public class MyClass: MyStaticTest {..}. Thus, all you can do with static class is to declare static members (static fields, properties, methods etc.)
The abstract keyword enables to create classes and class members that are incomplete and must be implemented in a derived class whereas The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.
I think its complete for Above
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I know that an interface inherits another interface, and a class can inherit from another interface, but can a class inherit from another interface?
Interfaces are meant to be implemented by classes. when you declare an interface you expect to work with an instance that implements it - that instance must be a class, since you cannot instantiate interfaces.
The case when you declare an interface which inherits from a different interface is possible, and allows you to separate the responsibility of each interface, but force a class to implement all of those responsibilities
As mentioned in my comment on the question, MSDN example for implementing interfaces HERE
It's called "implementing" the interface.
If interface Inter1{} and class A : Inter1, then if class B : A, class B will also "inherit" the interface Inter1.
From Microsoft's Inheritance (C# Programming Guide)
A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.
So, a class:
can implement multiple interfaces (no limits)
inherits from a base class (just the one. If none is declared, the base class is object by default)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a method in the Common class of a project I am working on which is defined like this:
public static void PopulateSoapBody<TEnum>(Object obj, string[] aMessage) where TEnum : struct, IComparable, IFormattable, IConvertible
and it is called like this (from several different classes defining their own enumerated types, and populating their own soap body classes) :
DCSSCardUpdateType wsSoapBody = new DCSSCardUpdateType();
Common.PopulateSoapBody<CardPinRequest>(wsSoapBody, aMessage);
where
CardPINRequest is an Enum Type defined in the calling class
wsSoapBody is a class type defined in the web service
aMessage is a string array (used to populate the wsSoapBody)
What is it called when an enum type is passed in like that to a method, restricting the possible types (I'd like to read up on it to understand better how to make use of features d like this)
I think the term you are looking for is generic type constraints.
From the linked MSDN article:
When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. If client code tries to instantiate your class by using a type that is not allowed by a constraint, the result is a compile-time error. These restrictions are called constraints.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to know the relation between instances of parent child in c#.
Suppose you have two classes Base and Derived:
Base b = new Base()
Derived d = new Derived()
Derived db = new Base()
Base bd = new Derived()
then what is the meaning of above types of objects
Please suggest I am confused.
Assuming that Base is actually the base class of Derived....
Line 1 contains an instance of the base class.
Line 2 contains a instance of the Derived class.
These are both "standard" variables, where the variable type exactly matches the instance it contains.
Line 4 is an instance of derived class, but restricted to only accessing base class methods. Think of it as a slightly restricted view of the derived class.
Line 3 should produce a compilation error. You can't place an instance of a base class in to a variable of a more restrictive type. Think of it like this "All Dogs(derived class) are Animals(base class), but not all Animals are Dogs.