I am in the beginners level to learn C#. Normally in C++ if we need to access the methods/values of the class we have to declare an object instance and thru object only we can access the method. But in C#.Net I came to know for static public class we dont need to declare the object, directly we can call the method using class.method() name. Could you pls clarify me when we refer the standard class library classes, such as messagebox.
System.Windows.MessageBox.ToShow()
System.Windows - Its name space.
ToShow - Its Method
MessageBox - Messagebox is class or object here?
I beleive for all the standard .NET class library namespaces internally create the object instances thru constructors when we refer any of the method belongs to the class. Its always object name is same as class name as its created by constructor. pls correct me if my understanding is wrong.
Thanks,
Karikalan
MessageBox is a class
http://msdn.microsoft.com/en-us/library/system.windows.messagebox.aspx
Show is a static method of MessageBox class. You can call static methods in the format ClassName.MethodName
Another thing about MessageBox is that it is a sealed class. A sealed class cannot be inherited. That means you can not derive a custom class from this class.
More info about Static classes and methods are nicely explained here
I beleive for all the standard .NET class library namespaces internally create the object instances thru constructors when we refer any of the method belongs to the class. Its always object name is same as class name as its created by constructor.
Not at all. Only static methods can be accessed directly from the class. To call an instance method, you need an instance of the class (i.e. an object).
System.Windows.MessageBox.Show();
System.Windows is namespace
MessageBox is a class
Show is a static method
MessageBox is a sealed class and it has a static method ToShow(). And to access static method you dont need to create instances.
Related
I have a class MainClass with method MainMethod. I created more classes which iherit from this class. I would like to add a line to MainMethod which will print the name of class of object from which this method was called. Is it possible?
Have you investigated CallerMemberNameAttribute and the other goodies discussed here:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/caller-information
I have noticed for high demand in my projects for objects such as textboxes or buttons public.
Is there any problems by setting them public?
What does public, private, static really mean?
Access Modifiers (C# Programming Guide)
public
The type or member can be accessed by any other code in the same
assembly or another assembly that references it.
private
The type or member can be accessed only by code in the same class or
struct.
protected
The type or member can be accessed only by code in the same class or
struct, or in a class that is derived from that class.
internal
The type or member can be accessed by any code in the same assembly,
but not from another assembly.
protected internal
The type or member can be accessed by any code in the assembly in
which it is declared, or from within a derived class in another
assembly. Access from another assembly must take place within a class
declaration that derives from the class in which the protected
internal element is declared, and it must take place through an
instance of the derived class type.
There is no security risk as far as I know. But there may be better alternative approach to design your program
public, private, etc are called access modifiers and determine the rules for which other code are allowed to access each member.
There is no technical problem of setting controls as public. But I would not recommend it. Having everything public is a good recipe for creating spaghetti code.
Keep all access to controls within your form and expose only a small set of public methods with a simple interface for external actors to access data and operations on the form.
You should not, for a clean design.
You should in reality put the logic of your application outside forms!
However, if you want to keep logic inside forms, you should at least expose them with public properties and methods, without giving direct access to form controls.
For example you can provide things like a method "EnableSave" or "QuitApplication" or "UpdateState".
Public, private and static deal with scope and what can talk to the objects / methods
Public -> Other classes can create an instance of your class (assuming the class is public) and call this object / method directly
Private -> Other classes can create an instance of your class (assuming the class is public) but can NOT access this object / method
Static -> Other classes can directly access this object / method (assuming class is public and method is public static) such as: YourClassName.ObjectOrMethod without having to create an instance of YourClassName
The best access modifier to give to a gui component when you want to access it directly is :internal (that is the default in VB.NET for example).
However you shouldn't give a public or internal modifier on a GUI control and you shouldn't access it directly, because the presentation layer and business logic layer should be kept separated in a well designed architecture ...
Hello All:
what is difference making full class static and making functions of the class as static?
I mean are there any performance issues?
The feature of static classes was only introduced in C# 2. The CLR doesn't really have such a concept. A static class in C#:
Has no constructors, not even a default parameterless one
Is sealed: it can't be subclassed
Is abstract: you can't create instances of it (which also follows from there being no constructors)
Can only contain static members
Can't be used as a parameter or variable type or a generic type argument etc.
Can't specify any base type other than object
There's no performance difference between using static members of a "normal" class and using ones in a static class. It's primarily a way of stating the intention that there should never be any instances of this class, without having to provide a private and never-called constructor, and forcing the compiler to check that you're not trying to use it as a normal class.
Note that extension methods (introduced in C# 3) can only be declared in a top-level, non-generic static class.
It has two implications:
All methods of the class must also be static.
You can't make an instance of that class other places in your code.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
When to Use Static Classes in C#
why anyone would write a static class. we can declare a static method in any class and just call that method without creating class instance. so please tell me in what type of situation a person would create a static class and also tell me what are the main differences between static class and normal class.
thanks
A static class cannot be instantiated. It's main uses are to make it clear that the class has no instance methods and to prevent people from accidentally trying to "new" the class.
Generally I would advise you not to write static classes.
There are cases where you want them though
Extension methods have to live on static classes. This is the best reason to have a static class.
If you do have a bunch of static methods that don't make sense as extension methods and don't fit into your object model then there might be room for a collection of static methods. This is particularly the case when you cannot redesign your app.
Sometimes this happens because you are dealing with some 3rd party stuff that you cannot change. Then if you end up with a class with only static methods on it - you should make it static since anyone creating an instance is clearly not understanding what you have done.
Having said all of that for the most part my advices is avoid static methods, classes and data. I am not saying never use them - just try not to.
A static class cannot contain any constructors, only a static constructor that is called first time one of its members is accessed.
That is basically the difference. Performance wise we also get another for free by the compiler since it can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.
Static classes are also sealed and therefore cannot be inherited.
http://msdn.microsoft.com/en-us/library/79b3xss3(v=VS.100).aspx
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type.
Another valid point is that Extension Methods has to be declared in a static class
http://en.wikipedia.org/wiki/Extension_method
The new language feature of extension methods in C# 3.0, however, makes the latter code possible. This approach requires a static class and a static method.
In static class all method all static. we can not declare normal method in static class.
differences between static class and normal class.
We can not create object of static class, when we use method of static class just use classname.MethodName but in normal class we have to first create object of class then we can access method of normal class.
static class ex.
Class1.functionname();
normal class ex.
Class1 cs = new class1();
cs.functionname();
functionname should not be private.
The difference is that you can't instantiate a static class. So you'd make a class static if you don't want it ever being instantiated. This is useful in cases when you're dealing with threading issues, and you want all threads to be guaranteed to use the same instance of your class.
A more philosophical scenario is when you have a class that doesn't need to be instantiated, such as (for example) you're building a database application and you create one class to do all the database access stuff. It's basically just a collection of methods. Then making the class static simply becomes a step to make your design more consistent.
I would like to access a class everywhere in my application, how can I do this?
To make it more clear, I have a class somewhere that use some code. I have an other class that use the same code. I do not want to duplicate so I would like to call the same code in both place by using something. In php I would just include("abc.php") in both... I do not want to create the object everytime I want to use the code.
Do you want to access the class or access an instance of the class from everywhere?
You can either make it a static class - public static class MyClass { } - or you can use the Singleton Pattern.
For the singleton pattern in its simplest form you can simply add a static property to the class (or some other class) that returns the same instance of the class like this:
public class MyClass
{
private static MyClass myClass;
public static MyClass MyClass
{
get { return myClass ?? (myClass = new MyClass()); }
}
private MyClass()
{
//private constructor makes it where this class can only be created by itself
}
}
The concept of global classes in C# is really just a simple matter of referencing the appropriate assembly containing the class. Once you have reference the needed assembly, you can refer to the class of choice either by it's fully qualified Type name, or by importing the namespace that contains the class. (Concrete instance or Static access to that class)
Or
You can have a Singleton class to use it everywhere but some people won't recommend you this way to proceed.
The other answers that you've been given about using a static class or a singleton pattern are correct.
Please consider, however, the fact that doing so does compromise your ability to test. In general, if you can, prefer dependency injection over globally accessed classes. I know this isn't always possible (or practical).
Just on that, you should also look up the abstract factory pattern. It allows you to have a well known factory class that produces the actual instance of a class that you're using. To have a globally accessed logging class, for example, don't directly create a logging class. Instead, use a logging factory to create it for you and return an interface to a logging class. That way it's easier to swap in and out different logging classes.
Since you do not want to create the object every time and it sounds like you are talking about some sort of utility methods...
I suggest you use static methods in an assembly which you can reference where needed