C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Introduction
So far we have seen
Identifiers and Keywords and
Literals, Operators and Punctuators in
C# Programming. Let's see static and instance members (also called as variables) in this chapter.
Objective
The main objective of this article is to learn about
static members,
instance members,
public keywords and
nested types in
C#.
Class Members
There are two types of Class Members (Variables) in C#. They are,
- Instance Members
- Static Members
Instance Members:
The members (data members and function members) which operate on the instance
of the type are called as Instance Members.
(or)
When a method, property, event, constructor etc. declarations does not include any static
modifier, then it declares an Instance Member.
Ex: ToString
method of int
.
These are instance by default.
Static Members:
The members (data members and function members) which doesn't operate on the instance
of the type are called as Static Members.
(or)
When a method, property, event, constructor etc. declarations include any static
modifier, then it declares an Static Member.
Ex: Console.WriteLine
. Basically Console is a static
class which has all static
members in it. One Console is enough to be shared across the entire application.
Let's have a simple code as example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Program
{
public string Name; // Instance field
public static int Number; // Static field
public Program(string n) // Constructor of the class Program
{
Name = n; // Assigning instance(Name) field
Number = Number + 1; // Incrementing static(Number) field
}
class Man
{
static void Main()
{
Program p1 = new Program("Abhinav");
Program p2 = new Program("Venkat");
Program p3 = new Program("Vishruth");
Console.WriteLine(p1.Name); // Abhinav
Console.WriteLine(p2.Name); // Venkat
Console.WriteLine(p3.Name); // Vishruth
Console.WriteLine(Program.Number); // 3
}
}
}
}
Now, observe the above code in which we have an
instance field Name that is an instance of the class
Program and a static field
Number.
We have a Constructor in the above code which creates instances of the class Program, prints their Names and then the total Number.
The output of the above code prints something like below,
Public Keyword:
A Keyword which exposes members to any other classes is known as a Public Keyword.
In the above example code, if the Name field was not public
, the Man class could not access it. Hence, public
says that, "I am public and I want other types (classes) to see me".
In object-oriented terms, we know that public
members encapsulate the private
members of the class.
Nested Types
A type or a class that is declared in another class or struct is called as Nested Type or Nested Class. And a class or type which is declared in namespace or compilation unit (which is outside any class) is known as Non-Nested Type.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nested
{
class One
{
class Two
{
public static void Main()
{
Console.WriteLine("This is a Nested Type");
}
}
}
}
In the above code, we have two classes
One and
Two
. Class
Two is declared within class
One. Hence, class
Two is called as
Nested Type (class) of
One.
Press Ctrl + F5 in to see the following result in your Console,
Conclusion
In this article, we have looked into static and instance members (variables), public keyword and Nested types in C#.Hope you understand.
Thank you for reading.
Regards,
Krishna.