The concepts that relate to the classes of C# come from other programming languages and will resume logic, while other constructs details have been designed to help programmers.
In the past, Windows programmers used the libraries (DLLs) to distribute the functions of their programs, there were two types of connection in order to export these methods, static and dynamic. This type of programming, however, led to problems of portability and discrepancy, in fact programs and libraries of different versions could not work. With the advent of .NET you have eliminated the static libraries and it was done in such a way that these dynamics were self-describing. In this way, the DLL uses both runtime and compile time, and linking. These DLLs include classes and components that were called assembly.
In object-oriented programming method can have visibility as private, protected and public, with the C# there is another construct: internal. With this particular construct the method is public within the assembly in which it is created, private if used by other assemblies.
Within a class can be declared as a static attribute, in this case, the compiler creates a single instance of the variable that will be common to all instances of the class. In this way it is possible to persist objects, we see a simple example that counts the number of times an object is instantiated.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestCons { class Test { static int nCounter = 0; public Test() { nCounter++; System.Console.WriteLine ("number of instances: " + nCounter); } } class Program { static void Main(string[] args) { Test test1 = new Test(); Test test2 = new Test(); Test test3 = new Test(); } } }
It may also be asked to create a variable only once in the program and make it non-editable throughout the session. In C# we have a special construct that allows you to do all this, it is readonly declare the instance variable that we intend to use for this purpose.
Another type of variable is const type, in this case we have a similar behavior as readonly, the only difference is that you can not assign a value to a const variable at runtime, this value must be known at the time of compilation. Another feature of const variables is that they are already active in memory before it is instantiated class that contains it.
In this article we saw some special constructs used inside classes in C#, they should make it possible to investigate the way in which they were developed components that are part of .NET and for this to develop better even our works.