static function ??????????????????

Posted by Surajemo under ASP.NET on 4/18/2012 | Points: 10 | Views : 1180 | Status : [Member] | Replies : 6
what happens if dont declare a function as static
and what type of code should be written in static function




Responses

Posted by: Sridevi on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
1. Static information will be loaded even before the constructor.
2. Static is the only entity which can be loaded by using both ways object reference and class reference. It means static method doesn't require objects . Ex: program.add()


Where can we use static function?

Information which can't be changed and information which is sharable and loaded intially. Ex: DB connection (pulling and pushing records) , web server banner, etc.....


Regards,
Sridevi

Sridevi

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kk86kiran on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Yes sridevi your right and one more pint is if you create any static method you can call that method directly by using class name.

For example in .net we have math class , it consists round,Max,Min and etc methods. So you can call directly those methods with the class name Math

Example:

System.Math.Round(33.44,1);

It will return 33.4

k.m.j.kiran

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Surajemo on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
ok thank u :)
sridevi can You give a small code on that the use of it in terms of database the one which u gave me Ex: DB connection (pulling and pushing records)

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sunny4989 on: 4/19/2012 [Member] Starter | Points: 25

Up
0
Down
static functions are functions that are only visable to other functions in the same file.

Different meanings

- When specified on a function declaration, it makes the function local to the file.
- When specified with a variable inside a function, it allows the vairable to retain its value between calls to the function.

------------------------------------------------
Learn throughout life

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sridevi on: 4/19/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Suraj,


This is the sample code.

using System;
using System.Collections.Generic;

class Program
{
static Stack<int> GetStack()
{
Stack<int> stack = new Stack<int>();
stack.Push(100);
stack.Push(1000);
stack.Push(10000);
return stack;
}

static void Main()
{
var stack = GetStack();
Console.WriteLine("--- Stack contents ---");
foreach (int i in stack)
{
Console.WriteLine(i);
}
}
}

Output

--- Stack contents ---
10000
1000
100


Regards,
Sridevi.

Sridevi

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/19/2012 [Member] Silver | Points: 25

Up
0
Down
See this for "when to use static..?"

http://forums.asp.net/t/1445372.aspx/1

Regards,
Singaravel M

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response