Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to define a function in c#

//The "void" in this instance refers to the return type of this function
//This function won't have a return value.
void functionName(int parameter) { 
  //Code you want to run
}
//Calls the code in the function
functionName(Aninteger)
Comment

c# funtion

public int AddNumbers(int number1, int number2){    int result = number1 + number2;    if(result > 10)    {    return result;    }    return 0;}
Comment

c# function

public void SayHello()
{
Console.WriteLine("Hello") ;
}
//as basic as it gets 
//for a function
Comment

function in c#

//function example
using System;
					
public class Program
{
	static void function(){
		Console.WriteLine("I am a function!");
	}
	public static void Main()
	{
		function();
	}
}
Comment

C# func

//Func is a invokeable container for Mehod or function method 
//that returns something. They can accept inputs based on the generals 
//that you template on and reserve the last general template for the return. 
//If you do not need a return or wish to return nothing check out the Action Class
//example:
Func<string/*Or some other type to return*/> YourFuncProperty
= new Func(()=>{/*do something*/ return "or return what you want";});
//or
Func<string/*Or some other type to return*/> YourFuncProperty
= ()=>{/*do something*/return "or return what you want";};
//for a paramiterized func
Func<int
/*Or some other types followed by others optional comma seperated*/,
string/*Or some other type to return last*/> YourParamitarizedFuncProperty =
  (x/*Each Param will be to the comma seperated types*/)=>
  {/*do some with the inputs*/return $"you entered a {x} or return what you want";};

// you can invloke them by calling their invokes.
string YouReturn = YourFuncProperty.Invoke();
string YouReturn = YourParamitarizedFuncProperty.Invoke(5);
//The last is the basic sycronous way. For a aysnc call uses
YourFuncProperty.BeginInvoke();
YourParamitarizedFuncProperty.BeginInvoke(5);
//however, you will need to begin a await with EndInvoke to get your result after.
string YouReturn = YourFuncProperty.EndInvoke();
string YouReturn = YourParamitarizedFuncProperty.EndInvoke(5);

//You can also fill them with defined methods in a class if you wish, 
//but the signatures must match.
Func<string> YourActionDefinedProperty = YourDefinedMethod;
string YourDefinedMethod()
{
  //do something
  return "or return what you want";
}

//Example of use
public sealed class DataContainer
{
  	//A bit contrived but we will give the ablity to overide the printout 
  	//of the class while still keeping it sealed. See the invoke at ToString.
  	//could be useful in a library or something?
	static func<string> SealedFuncStringOverride;

	DataContainer(datetime Date)
    {
      this.Date = Date;
      
    }
  	public datetime Date {get; private set;}
  	public int Amount {get; private set;}
  	public string Info {get; private set;}
  	public string FirstName {get; private set;}
  
  	//The invoke is used in here.
  	public override string ToString()
   	{
      if(SealedFuncStringOverride!=null)
        return SealedFuncStringOverride.BeginInvoke();
      return base.ToString;
   	}
}
Comment

how to create function in c#

        public static string name_of_the_fucntion()
        {
            return "string";
        }
Comment

function c#

public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    return result;
}
Comment

c# func

// Func<T,TResult>
// example:
Func<int, string> foo = ConvertToString;
private string ConvertToString(int input) {
 	return input.ToString(); 
}
// or
private void Bar(Func<int, string> bar) {
	Console.WriteLine(bar(myInt));
}
Comment

how to create new function c#

public void DoStuff()
{
    Console.WriteLine("I'm doing something...");
}
Comment

C# functions

// In C#, this is how we define a function:
/*
In this case, the function is called 'myFunction', it takes
1 parameter which is an integer and doesn't return anything (void).
*/
static void myFunction(int a)
{
  // INSERT CODE HERE
  Console.WriteLine(a);
}
Comment

c# func

using System;
using System.Collections.Generic;
using static System.Console;
namespace CSFlow.Delegates.Others
{
    public class ActionFunc
    {

        public static void Run ()
        {
            // Example of : Action
            // Use action when a mathod return void
            Action<string> display = new Action<string>(DisplayMessage);
            display("Calculate Discount :");

            // Example of : Func
            // Use action when a mathod return value
            Func<double, double> discount = new Func<double, double>(Discount);
            display(discount(12.5).ToString());

            List<Customer> custList = new List<Customer>();
            custList.Add(new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", State = "Telengana", City = "Hyderabad", Address = "Begumpet", Country = "India" });
            custList.Add(new Customer { Id = 2, FirstName = "Steve", LastName = "Jones", State = "OA", City = "New York", Address = "Lake Avenue", Country = "US" });
            custList.Add(new Customer { Id = 3, FirstName = "Sefat", LastName = "Anam", State = "OSA", City = "New York", Address = "Manhatten", Country = "US" });

            // Example of : Predicate
            // Use Predicate for search data
            Predicate<Customer> FindAddress = customer => customer.Address == "Manhatten";

            Customer searchData = custList.Find(FindAddress);

            display($"{searchData?.FirstName} {searchData?.LastName} From - {searchData?.City} ");

            ReadKey();
        }

        static void DisplayMessage (string message)
        {
            WriteLine(message);
        }

        static double Discount (double money)
        {
            return money * .5;
        }

        class Customer
        {
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Country { get; set; }
        }
    }
}
Comment

functions c#

public void SayHello(string name) 
{
    Console.WriteLine("Hello");
}

public void SayName()
{
	Console.WriteLine("What is your name?");
	string name = Console.ReadLine(); 
	SayHello(name);
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: split lines c# 
Csharp :: input field to float unity 
Csharp :: vb.net remove last comma from string 
Csharp :: unity render to texture2d 
Csharp :: how to set border for groupbox in c# 
Csharp :: adding a dependency injection service in windows forms app 
Csharp :: c# replace multiple characters 
Csharp :: how to set the value of a textbox textmode=date asp.net c# 
Csharp :: unity draw ray from one object to another 
Csharp :: c# modify dictionary in loop 
Csharp :: c# substring until character single 
Csharp :: destroy gameobject with tag unity 
Csharp :: switch statement c# example 
Csharp :: singleton design pattern c# volatile 
Csharp :: shut game unity 
Csharp :: migrationbuilder insert data example 
Csharp :: get ad user using email address microsoft graph C# 
Csharp :: string c# 
Csharp :: frustum 
Csharp :: searching for keys in the registry 
Csharp :: c# shorthand if statement without else 
Csharp :: convert string to decimal c# 
Csharp :: c# null conditional 
Csharp :: how to change color of part from the text in textblock wpf 
Csharp :: listview thread error 
Csharp :: pubxml environment variables 
Csharp :: unity draw waypoins path 
Csharp :: C# top down view player movement 
Csharp :: unity line renderer opacity 
Csharp :: print hello world in unity 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =