sealed

TypeConverter Attribute for Third Party Classes

别来无恙 提交于 2019-12-08 14:44:42
问题 When creating a class, a TypeConverter attribute can be applied to it s.t. using TypeDescriptor.GetConverter(typeof(T)) return the custom type converter. For instance: [TypeConverter(typeof(FooConverter))] public class Foo {...} public class FooConverter: TypeConverter {...} var tc = TypeDescriptor.GetConverter(typeof(T)); //returns a FooConverter instance. This works as long as the class is of our making. But how would one provide a custom TypeConverter for a class which we cannot modify the

Sealed property of abstract class

时光怂恿深爱的人放手 提交于 2019-12-07 16:53:56
问题 Please consider the following design: public interface IBook { string Author { get; set; } string Title { get; set; } } abstract class BaseBook : IBook { private string _title; public virtual string Author { get { Console.WriteLine("Base book GET!"); return _title; } set { Console.WriteLine("Base book SET!"); _title = value; } } public string Title { get; set; } } class Book : BaseBook { } class SuperBook : Book { public override string Author { get { Console.WriteLine("SuperBook GET!");

Is there an alternative to inheriting from sealed classes?

蓝咒 提交于 2019-12-07 10:31:49
问题 The reason why I'm asking this is because I want to create a class that has all the functionality of the FileInfo class (derives from FileInfo), and allows me to add my own properties to it. I Think an example will collaborate more. What I want: BindingList<FileInformation> files = new BindingList<FileInformation>(); public void GatherFileInfo(string path) { files.Add(new FileInformation(path)); listboxFiles.DataContext = files; } class FileInformation : FileInfo { public bool selected =

How to override functions from String class in C#

一世执手 提交于 2019-12-07 04:23:11
问题 For example, I need to see if a string contains a substring, so I just do: String helloworld = "Hello World"; if(helloworld.Contains("ello"){ //do something } but if I have an array of items String helloworld = "Hello World"; String items = { "He", "el", "lo" }; I needed to create a function inside the String class that would return true if either of the items inside the array is contained in the string, for example. I would like to override the function Contains(string) with Contains

Abstract Sealed Classes

痞子三分冷 提交于 2019-12-07 04:15:39
问题 Just a small question about c++/cli. Abstract classes have abstract methods to be implemented by derived classes, sealed classes dont allow inheritance. So why we have some classes in .NET base class library defined as abstract sealed, and you can find plenty .. ??! 回答1: It is equivalent to "static class" in the C# language. The language that was used to write almost all of the BCL classes. All the methods must be static. Declaring it abstract and sealed prevents anybody from deriving from

Why exposed types must be sealed for WinMD/WinRT components?

≡放荡痞女 提交于 2019-12-07 00:23:06
问题 VS compiler does not allow to create sealed exposed types for WINMD type library. Why is this restriction placed ? (I know about sealed types advantages, my question is with respect to Win RT components). 回答1: This is an architectural limitation, imposed by COM. Which sits at the core of any WinRT type, they are derived from IUnknown and IInspectable. The problem with COM is that it only supports interface inheritance but not implementation inheritance. Which was a strong COM design goal,

Sealed property of abstract class

筅森魡賤 提交于 2019-12-06 02:42:54
Please consider the following design: public interface IBook { string Author { get; set; } string Title { get; set; } } abstract class BaseBook : IBook { private string _title; public virtual string Author { get { Console.WriteLine("Base book GET!"); return _title; } set { Console.WriteLine("Base book SET!"); _title = value; } } public string Title { get; set; } } class Book : BaseBook { } class SuperBook : Book { public override string Author { get { Console.WriteLine("SuperBook GET!"); return base.Author; } set { Console.WriteLine("SuperBook SET!"); base.Author = value; } } public string

How to override functions from String class in C#

拜拜、爱过 提交于 2019-12-05 10:43:57
For example, I need to see if a string contains a substring, so I just do: String helloworld = "Hello World"; if(helloworld.Contains("ello"){ //do something } but if I have an array of items String helloworld = "Hello World"; String items = { "He", "el", "lo" }; I needed to create a function inside the String class that would return true if either of the items inside the array is contained in the string, for example. I would like to override the function Contains(string) with Contains(IEnumerable) for this scenario, instead of creating a function in another class. Is it possible to do this,

Abstract Sealed Classes

↘锁芯ラ 提交于 2019-12-05 09:59:31
Just a small question about c++/cli. Abstract classes have abstract methods to be implemented by derived classes, sealed classes dont allow inheritance. So why we have some classes in .NET base class library defined as abstract sealed, and you can find plenty .. ??! It is equivalent to "static class" in the C# language. The language that was used to write almost all of the BCL classes. All the methods must be static. Declaring it abstract and sealed prevents anybody from deriving from the class and creating an instance of it. The class methods are the exact equivalent of free functions in the

Preventing a method from being overridden in C#

我的未来我决定 提交于 2019-12-05 09:47:26
问题 How do I prevent a method from being overridden in a derived class? In Java I could do this by using the final modifier on the method I wish to prevent from being overridden. How do I achieve the same in C#? I am aware of using sealed but apparently I can use it only with the override keyword? class A { public void methodA() { // Code. } public virtual void methodB() { // Code. } } class B : A { sealed override public void methodB() { // Code. } } So in the above example I can prevent the