This question already has answers here:
Why do members of a static class need to be declared as static? Why isn't it just implicit?
(8 answers)
Closed 7 years ago.
My question is why members are not by default static in static class.
As we see, interface members are by default public and abstract.
Thanks,
Anil
We should ask c# language designer.
But I understand the ratio behind: it forces the programmer to say "this function is static", even if it would be implied by the fact the the class is static.
Maybe It's a matter of readability: when you read a method without body (and no abstract keyword), you know that this method can only be part of an interface.
When you read a method without "static" modifier, you would need to read also class declaration to understand that is part of a static class and therefore static itself
Related
This question already has answers here:
C# virtual keyword
(9 answers)
C# - Keyword usage virtual+override vs. new
(11 answers)
Can you write virtual functions / methods in Java?
(6 answers)
Closed 4 years ago.
In an interview it was asked why do we need to override method of base class.
I tried to answer like when we want to have different implementation in derived class.
But then he said, "Why don't we just create a new method with different name and different implementation instead of overriding base class method?", anyway we are not reusing base class method as implementation will be different then just create a new method instead of overriding.
I got confused what to answer. Could somebody explain.
public class BaseClass
{
virtual void Foo(){}
}
public class DerivedClass: BaseClass
{
override void Foo(){}
}
Generally we implement overriding like above.
What he said is like why do we need concept of overriding we can do like below
public class BaseClass
{
void Foo(){}
}
public class DerivedClass: BaseClass
{
void Foo1(){}
}
His question was looking weird I tried to explain but like its a method of base class we are just redefining it in derived class. In this way our code will be clean as well. But looks like he was not satisfied.
I would check this answer:
Why does this polymorphic C# code print what it does?
then try to grasp the concept of methods in an object basically having pointers to code. When you override implementation in an subclass then that becomes the new code pointed to, whether it's used or cast as a superclass or not.
So the main purpose of overriding is to create classes that inherit from one class but each have their own implementation and then be able to treat or operate on them equally the same as the original superclass. This is the essence of the Liskov Principle or the 'L' in SOLID. What is an example of the Liskov Substitution Principle?
This question already has answers here:
Why does my program work if my Main method in C# is private?
(4 answers)
Closed 4 years ago.
If internal class can only be accessed by anywhere in same assembly and it can't be accessed outside the assembly how Main() method is called by CLR?
using System;
namespace test
{
internal class Program {
public static void Main(String[] args){
Console.WriteLine("Testing Internal Modifier!!");
Console.ReadLine();
}
}
}
Thanks.
Private, protected, internal etc. modifiers are there to keep your code clean and help you make less errors. Other than that, everything is still callable, those modifiers are in no way a security tool to prevent the CLR or even other code from calling your code.
Using reflection you can call every single method in your class, be it private, internal or public. You can even declare Main() as private, it'll still be the entry point to your application.
This question already has answers here:
What does the "private" modifier do?
(13 answers)
Closed 7 years ago.
Is this definition for a private access modifier accurate?:
private: Any thing declared as private can’t be seen outside of its class.
What about a private nested class?
This class can be seen "outside of its own class" by the class that is nesting it.
Maybe you could say that this nested class is actually a private member of the class nesting it and therefore can't be seen outside of the nesting class which would make the definition provided above true.
If a private nested class is NOT considered a member of the nesting class then I think the definition provided above is not accurate.
Maybe you could say that this nested class is actually a private member of the class nesting it and therefore can't be seen outside of the nesting class.
That's exactly what it means.
This question already has answers here:
When to use static classes in C# [duplicate]
(11 answers)
Closed 9 years ago.
I am a C++ programmer who makes a switch to C#.I was said not to use static classes.
I understand that in C# if the class has only static members it should be static.(My class has only static members)
Can you please explain me what could be a problem using static classes?
When we really should use them?
Thanks
The main reason is that sometimes (but NOT always), it's better to use a singleton class, because a singleton class can implement interfaces.
Static classes have no difference from non-static classes apart from the fact that you can't instantiate static classes (because they're static of course) and this static class will not be able to take advantage of cool OOP features such as inheritance (it can't be subclassed in C#).
The main thing you have to be aware of when a static class contains static members is making the class static members thread-safe if used by multiple threads.
Can you please explain me what could be a problem using static classes? When we really should use them?
If your class only exposes static members then you can make it a static class, but only if you are sure you will not need instances of this class. In fact, there's absolutely no issues in using static classes, just make then thread-safe (if required) and make sure it fits in with your system's design.
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.
http://msdn.microsoft.com/en-us/library/79b3xss3.aspx
Check also this link:
When to use static classes in C#
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can’t I create an abstract constructor on an abstract C# class?
Can abstract class's Constructor be marked as 'abstract'?
No. C# does not support this in any version. Note that constructors are not inherited in derived classes, although they can be "chained". This is probably what you want to be doing.
If you want to indicate that the derived class should be doing some sort of initialisation, you could create an abstract Initialise method or such which the constructor of the base class (and indirectly of the sub-class) calls on creation.
As a side point, I'm not sure whether the CLR (or associated CIL language) actually supports it - I would suspect it may, though there is little use for it from within C# for the reason just mentioned.
No, a constructor cannot be marked as abstract. In abstract classes constructors are usually marked as protected though, at least that's what I would recommend you doing.
Basically no.
If its abstract, you have to override it in a concrete child class, and you can't override a constructor, only overload it.