Computer Science, asked by vipingogo7256, 1 year ago

When do we declare a member of a class static? Why? Discuss static member function with example?

Answers

Answered by lovingheart
6

When we do not want to create an instance to access the members of the class then we declare the class as static. It is also sealed, so further these cannot be inherited.

Example of built in static class: System.Math (The programmer need not create instance to access this class)

Example of static built in method: Math.Abs(7.5), Math.Floor(9.2), etc

public static class Temperatureconversion

{

public static double ctof(string celsius)

{

 double c = Double.Parse (celsius);

 double f = (c * 9/5)+32;

 return f;

}

}

class staticexample

{

static void Main()

{

 double f, c = 0;

 Console.Write(“Please enter temperature in Celsius”);

 f = Temperatureconversion.ctof(Console.ReadLine());

 Console.WriteLine(f);

}

}  

Similar questions