what static fuction mean in c#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

when i was usong c++ idont have to put static before tha function that i want
to acces but in c# i have to but it .
ex
:
ihave a function called add(int x,int y)
i have to put
static int add(int x,int y) in prototype
or i have this
"an object refrence is required for the nonstatic field, methiod or property"

i hope someone to tell me why as i think the idea of static in c# is not
like in c++
or tell me where to post this

thanx at all
 
when i was usong c++ idont have to put static before tha function that i
want
to acces but in c# i have to but it .

Well, in both languages you have to declare a member function as static if
you want to call that function without an instance of the class - it's the
same in both languages. In C++, like in C#, you need an instance to call a
nonstatic member function. I don't see why you think this would be
different?
 
Guido Stercken-Sorrenti said:
Well, in both languages you have to declare a member function as static if you want to call that
function without an instance of the class - it's the same in both languages. In C++, like in C#,
you need an instance to call a nonstatic member function. I don't see why you think this would be
different?

The difference is that in C++ you can declare functions outside of any class. (like in C)
In C# all functions/methods must be part of a Class.
If you don't declare the method as static it means that it is associated with a particular OBJECT.
If you do declare the method as static it means that it is associated with the CLASS.

Bill
 
hi,

yesterday there was a thread about this, named "static methods" it may be of
help


cheers,
 
Guido Stercken-Sorrenti said:
Well, in both languages you have to declare a member function as static if
you want to call that function without an instance of the class - it's the
same in both languages. In C++, like in C#, you need an instance to call a
nonstatic member function. I don't see why you think this would be
different?
iam not talking about member function in class
in c++ i prototype the function and call it without give me that error
maybe as bill say because of every main have to be in class .
amd i see this because i try it!
 
First, in c# every there is no soch thing as a function. there are methods.

Every method must be a member of a class -

for example:
======================
namespace foo
{
int Add(int x,int y){ return x+y;}
}
======================

will not compile because, in C# "A namespace does not directly contain
members such as fields or methods"

but this does:
======================
namespace foo
{
class Foobar
{
int x = 0;
public Foobar(int n){ x = n;}
public int Add(int y){ return x+y;}
}
}
======================

As the 'Add' method is a member of the class Foobar which is 'directly
contianed' in the foo namespace. But to use it, you need an instance of
Foobar:

for example:
======================
Foobar foobar;
foobar.Add(4);
======================
won't compile: "unassigned local variable 'foobar'"


Now, with regards to static -

A method modified by the keywords 'static' in C# means that you can call the
method without instancing the containing class.

Lets modify the foobar class above:

======================
namespace foo
{
class Foobar
{
int x = 0;
public Foobar(int n){ x = n;}
public int Add(int y){ return x+y;}
public static int Add(int x, int y){ return x+y;}
}
}
======================

this line will work without an actual instance of Foobar:
======================
System.Console.WriteLine(Foobar.Add(1,2));
======================

and it is 'roughly the equivalent of:
======================
Foobar foobar = new Foobar(1);
System.Console.WriteLine(foobar.Add(2));
======================

Now, static methods cannot reference non-static members, or 'Instance
Members' of their containing class.

for example:
======================
namespace foo
{
class Foobar
{
int x = 0;
public static int Add(int y){return x+=y;}
}
}
======================

does not compile: "An object reference is required for the nonstatic field,
method, or property 'foo.Foobar.x'"

x is only available within actual instances of a foobar class where the
static Add method does not belong to the instance but the class definition.

But, you can mark x as static and it becomes available:
======================
namespace foo
{
class Foobar
{
static int x = 0;
public static int Add(int y){return x+=y;}
}
}
======================

The following does compile as x is declared a static member of Foobar. But
tis has a side effect. x belongs now to ALL instances of Foobar changing one
x, changes x through out the application:

======================
System.Console.WriteLine(Foobar.Add(2));
System.Console.WriteLine(Foobar.Add(2));
System.Console.WriteLine(Foobar.Add(4));
======================

outputs:
======================
2
4
8
======================
as the static x is modified on each call.

make sense????
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top