Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Static and nonstatic in C#!

Status
Not open for further replies.

Sajjadkhan

Full Member level 5
Joined
Sep 25, 2010
Messages
307
Helped
17
Reputation
34
Reaction score
16
Trophy points
1,298
Location
Rawalpindi,Pakistan
Activity points
4,199
Hi guys, i have stated learing C# and i am confused about these terms.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Functions_basics_1
{
    class Example
    {
        public int abc;
        public static int xyz;
        public void memberfunction()
        {
            Console.WriteLine("this is a member function");
        }
        public static void classfunction()
        {
            Console.WriteLine("this is a class function");
        }
        static void Main(string[] args)
        {
            Example example = new Example();
            example.memberfunction();
            Example.classfunction();

            example.classfunction(); // cant access class function via an object
            Example.memberfunction(); // cant access member function via class

            Console.Read();
        }
    }
}


Now i know whats happening here as i commented in last lines of code. same example i encountered in previous but with integer, over looked and didn't bother searching for its use. but i think this will bug me ahead.

so kindly tell me whats the advantages of static and nonstatic? any realtime example would be helpful,thanks.
 

Have you noticed that main() itself is declared as static?
It is because main() is called by the OS without being an object is created.

Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member.

A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.

Whenever a process is loaded in the RAM, we can say that the memory is roughly divided into three areas (within that process): Stack, Heap, and Static (which, in .NET, is actually a special area inside Heap only known as High Frequency Heap).

The static part holds the “static” member variables and methods. What exactly is static? Those methods and variables which don't need an instance of a class to be created are defined as being static. In C# (and Java too), we use the static keyword to label such members as static.


In your code the object reference is "example".
The member function is called as:-
example.memberfunction();
where as:-
static function is called as :-

Example.classfunction(); //Here "Example" is a class name


Here is an example :-


Suppose you have a class CompanyInfo that contains the following methods to get information about the company name and address.



Code C# - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Automobile
{
    public static int NumberOfWheels = 4;
    public static int SizeOfGasTank
    {
        get
        {
            return 15;
        }
    }
    public static void Drive() { }
    public static event EventType RunOutOfGas;
 
    //other non-static fields and properties...
}



Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member. For example:


Code C# - [expand]
1
2
Automobile.Drive();
int i = Automobile.NumberOfWheels;



This example reads the name and ID of a new employee, increments the employee counter by one, and displays the information for the new employee as well as the new number of employees. For simplicity, this program reads the current number of employees from the keyboard. In a real application, this information should be read from a file.


Code C# - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// cs_static_keyword.cs
using System;
 
public class Employee
{
    public string id;
    public string name;
 
    public Employee()
    {
    }
 
    public Employee(string name, string id)
    {
        this.name = name;
        this.id = id;
    }
 
    public static int employeeCounter;
 
    public static int AddEmployee()
    {
        return ++employeeCounter;
    }
}
 
class MainClass : Employee
{
    static void Main()
    {
        Console.Write("Enter the employee's name: ");
        string name = Console.ReadLine();
        Console.Write("Enter the employee's ID: ");
        string id = Console.ReadLine();
 
        // Create and configure the employee object:
        Employee e = new Employee(name, id);
        Console.Write("Enter the current number of employees: ");
        string n = Console.ReadLine();
        Employee.employeeCounter = Int32.Parse(n);
        Employee.AddEmployee();
 
        // Display the new information:
        Console.WriteLine("Name: {0}", e.name);
        Console.WriteLine("ID:   {0}", e.id);
        Console.WriteLine("New Number of Employees: {0}",
                      Employee.employeeCounter);
    }
}



WriteLine() method is declared as public static void WriteLine() in Console class. As it is declared as static, we can use it like Console.WriteLine ; without creating an object.
 
Last edited:
Ok i know roughly what object is! like Class is a blue print of a house, but its not a house, a house built on that blue print is an object, because now it exixts. the Os calls main without an object being created. are you saying that when object is created than it takes memory else it wont? if it wont then in that main() if i write a for loop for(int count =0;count<10;count++) that count has took memory or not?
 

Whenever a process is loaded in the RAM, we can say that the memory is roughly divided into three areas (within that process): Stack, Heap, and Static (which, in .NET, is actually a special area inside Heap only known as High Frequency Heap).

The static part holds the “static” member variables and methods. What exactly is static? Those methods and variables which don't need an instance of a class to be created are defined as being static. In C# (and Java too), we use the static keyword to label such members as static.

- - - Updated - - -

When you create an instance of a class (not a struct or an enum), your object is being stored on the "heap" - a large contiguous area of memory that is just there.

The for loop variable will be local to main() and will occupy memory.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top