Ticker

6/recent/ticker-posts

C# (C Sharp) - Functions | Static Method

C# (C Sharp) - Functions | Static Method

In C#, a function is a way of packaging code that does something and then returns the value. Unlike in C, C++ and some other languages, functions do not exist by themselves. They are part of an object-oriented approach to programming 

In C#, a function can be called a member function—it is a member of a class—but that terminology is left over from C++. The usual name for it is a method. 

A simple example - The code below adds a function, a method that outputs the word "Hello." 

using System; 

namespace funcex1 

class Test 

public void SayHello() 

Console.WriteLine("Hello") ; 

class Program 

static void Main(string[] args) 

var t = new Test() ; 

t.SayHello() ; Console.ReadKey() ; 

Static Method 

Static methods are called without instantiation. This means that static methods can only access other static members of the class 

Syntax 

public class className 

modifier static dataType methodName (inputParameters) 

//static method 

//block of code to be executed 

}

//calling the method, from anywhere 

className.methodName(passedParams); 

Example 

public class Numbers 

public Numbers() {} 

public static int findMinimum(int number1, int number2) 

//3 stored in number1, 5 stored in number2 

int minimum = number2; 

if (number1 < number2) minimum = number1; 

return minimum;

}

int min = Numbers.findMinimum(3, 5); 

C# Interface:

  • An interface in C# contains only the declaration of the methods, properties, and events, but not the implementation. It is left to the class that implements the interface by providing implementation for all the members of the interface. Interface makes it easy to maintain a program. 
  • In C#, an interface can be defined using the interface keyword. For example, the following is a simple interface for a logging string message:

Interface Declaration: 

interface ILog 

void Log(string msgToLog); 

1. Now, different classes can implement ILog by providing an implementation of the Log() method, for example, the ConsoleLog class logs the string on the console whereas FileLog logs the string into a text file. 

Implement interface using  <Class Name> : <Interface Name> syntax. 

Implement Interface 

class ConsoleLog: ILog 

public void Log(string msgToPrint) 

Console.WriteLine(msgToPrint); 

}

class FileLog :ILog 

public void Log(string msgToPrint) 

File.AppendText(@"C:\Log.txt").Write(msgToPrint); 

}

2. Now, you can instantiate an object of either the ConsoleLog or FileLog class: 

Instantiate Object 

ILog log = new ConsoleLog(); 

//Or 

ILog log = new FileLog(); 

Dispose method

  • You implement a Dispose method to release unmanaged resources used by your application. The .NET garbage collector does not allocate or release unmanaged memory. 
  • The pattern for disposing an object, referred to as a dispose pattern, imposes order on the lifetime of an object. The dispose pattern is used only for objects that access unmanaged resources, such as file and pipe handles, registry handles, wait handles, or pointers to blocks of unmanaged memory. This is because the garbage collector is very efficient at reclaiming unused managed objects, but it is unable to reclaim unmanaged objects. 

Example Program ?? 

C# Operator Overloading 

  • The concept of overloading a function can also be applied to operators. Operator overloading gives the ability to use the same operator to do various operations. It provides additional capabilities to C# operators when they are applied to user-defined data types. 
  • The function of the operator is declared by using the operator keyword. 

Syntax: 

access specifier className operator Operator_symbol (parameters) 

// Code 


// C# program to illustrate the unary operator overloading 

using System; 

namespace Calculator 

class Calculator 

public int number1 , number2; 

public Calculator(int num1 , int num2) 

number1 = num1; 

number2 = num2;

// Function to perform operation 

// By changing sign of integers

public static Calculator operator -(Calculator c1) 

c1.number1 = -c1.number1; 

c1.number2 = -c1.number2; 

return c1;

}

// Function to print the numbers 

public void Print() { 

Console.WriteLine ("Number1 = " + number1); 

Console.WriteLine ("Number2 = " + number2);

}

}

class EntryPoint 

// Driver Code 

static void Main(String []args) 

// using overloaded - operator 

// with the class object 

Calculator calc = new Calculator(15, -25); 

calc = - calc; 

// To display the result 

calc.Print(); 

Output: 

Number1 = -15 

Number2 = 25 


"when to use static class in c#"

"static method in c#"

"static method in c# example"

"static class in c# with example"

"how to call a static method in c#"

"c# static constructor"