Home Functions Commands

ESharper ESharper

Function examples and sample code

1. Using LINQ.

LINQ example
using System.Linq;

public class C
{
	public static string FirstName(string fullName)
	{
		return fullName.Split(' ').First();
	}
}

2. Accepting one-dimensional range.

One-dimensional range example
public class C
{
	public static string CommaSeparated(object[] range)
	{
		return string.Join(", ", range);
	}
}

3. Accepting two-dimensional range.

Two-dimensional range example
using System.Linq;

public class C
{
	public static int DistinctCount(object[,] range)
	{
		return (from string s in range select s).Distinct().Count();
	}
}

4. Using Excel object model.

Using ExcelDna
public class C
{
	public static bool IsLeapYear(int year)
	{
		return System.DateTime.IsLeapYear(year);
	}

	public static int CountIfFunc(object[,] range, string functionName)
	{
		dynamic xlApp = ExcelDna.Integration.ExcelDnaUtil.Application;
		int result = 0;
		foreach(var i in range)
		{
			if(xlApp.Run(functionName, i))
				++result;
		}
		return result;
	}
}

5. Using regular expressions.

Using regular expressions
public class C
{
	public static bool RegexIsMatch(string input, string pattern)
	{
		return System.Text.RegularExpressions.Regex.IsMatch(input, pattern);
	}
}

6. Providing descriptions for the Excel function wizard.

Providing descriptions
public class C
{
	[ExcelDna.Integration.ExcelFunction("Returns the full name of a person")]
	public static string ESharperFullName(
		[ExcelDna.Integration.ExcelArgument("First name")] string fname, 
		[ExcelDna.Integration.ExcelArgument("Last name")] string lname)
	{
		return fname + " " + lname;
	}
}

To show help as you type, install Excel-DNA IntelliSense add-in:

Providing IntelliSense