Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Generic Delegates in C#

Generic Delegates in C#

"Like generic classes and generic methods, a delegate can define its own type parameters"

Following example explained Generic delegate with type parameters. I. e delegate T SampleMatOperations<T>

Generic Delegate in C# with Real-Time Example

//Delegate declaration
public delegate T SampleMathOperations<T>(T x, T y);

//Math operations class 
public class MathOperations
    {
        public int Addition(int x,int y)
        {
            return x + y;
        }
 
        public int Subtract(int a,int b)
        {
            return a - b;
        }
    }


Create instance and invoke generic delegate

class Program
    {
        static void Main(string[] args)
        {
            int a = 50;int b = 75;
            var matOpObject = new MathOperations();
 
            //Generic delegate instance
            //additions
            SampleMathOperations<int> mathOperations = new SampleMathOperations<int>(matOpObject.Addition);
            //invoke 
            Console.WriteLine("Add  result on two numbers"
               + mathOperations(a, b));
 
            //subtraction (multicast delegate)
            mathOperations += matOpObject.Subtract;
            //invoke 
            Console.WriteLine("Sub result on two numbers"
               + mathOperations(a, b));
        }
    }


Inbuilt generic delegates

Microsoft provided three inbuilt generic delegates delegates introduced in C# 3 “System namespace”, those are Func, Action, and Predicate.
All these three delegates can be used with the method, anonymous method, and lambda expressions.

Func generic delegate in C#

When to use?

“Whenever delegates return some value with optional input parameters. i.e C# method with return value”

Description

This delegate takes one or more input parameters and returns one out parameter. The last parameter is considered as the return value.

It can take up to 16 input parameters of different types and those are optional, it must have one return type that is mandatory.

Action generic delegate in C#

When to use?

“Whenever delegates do not return any value with optional input parameters. i.e. C# method with void return value”

Description

It takes one or more input parameters and returns nothing.

This delegate can take a maximum of 16 input parameters of the different or same type.

Predicate Generic Delegate in C#

When to use?

“Whenever delegates return a Boolean value with optional input parameters. i.e. C# method with Boolean return value”

Description

It takes one input parameter and always returns a Boolean value which is mandatory.

This delegate can take a maximum of 1 input parameter and always return the value of the Boolean type.


Inbuilt Generic Delegates in C# with Real-Time Examples

Sample C# class with methods

public class GenericDelegatesSample
    {
        public int Addition(int x, int y)
        {
            return x + y;
        }
 
        public bool IsValidNumber(int x)
        {
            return x > 0 ? true : false;
        }
 
        public void DisplayResult(int x,int y)
        {
            var result = Addition(x, y);
            Console.WriteLine("Addition is :" + result);
        }
    }


Create instance of Inbuilt Generic delegates and invoke

class Program
    {
        static void Main(string[] args)
        {
            int a = 50;int b = 75;
            var opObject = new GenericDelegatesSample();
           
            //Func generic delegate
            Func<int, int, int> add = opObject.Addition;
            //invoke
            Console.WriteLine("Display result :" + add(a,b));
           
            //Action generic delegate
            Action<int, int> displayResult = opObject.DisplayResult;
            //invoke             displayResult(a, b);
 
            //Predicate generic delegate
            Predicate<int> isValidNum = opObject.IsValidNumber;
            //invoke
            Console.WriteLine("is valid number:" + isValidNum(a));
        }
    }

 

Add active class to layout bootstrap navbar in MVC

How to add an active class to my bootstrap navbar in MVC with?

Hers is code example,

Whenever nav links clicked "navHelperExtension" class MakeActiveClass method adds active class to nav list item.

<ul id="topMenu" class="navbar-nav">
<li id="navHome" class="@navHelperExtension.MakeActiveClass("Index", "Home")">
<a id="acHome" class="nav-link" href="@Url.Action("Index", "Home")">Home </a>
</li>
<li id="navAbout" class="@navHelperExtension.MakeActiveClass("About","Home")">
<a id="acAbout" class="nav-link" href="@Url.Action("About", "Home")">About us </a>
</li>
<li id="navTeam" class="@navHelperExtension.MakeActiveClass("Team","Details")">
<a id="acTeam" class="nav-link" href="@Url.Action("Team", "Details")">Our Team</a>
 </li>
<li id="navContact" class="@navHelperExtension.MakeActiveClass("Contact","Details")">
<a id="acContact" class="nav-link" href="@Url.Action("Contact", "Details")">Contact </a>
 </li>
</ul>

C# static class and method to make current selected controller link active

public static class
navHelperExtension
    {
  public static string MakeActiveClass(string controller)
        {
            System.Web.Mvc.UrlHelper urlHelper= ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Url;
 
            string result = "nav-item active";
 
       string controllerName =urlHelper.RequestContext.RouteData.Values["controller"].ToString();
 
            if (!controllerName.Equals(controller, StringComparison.OrdinalIgnoreCase))
            {
                result = "nav-item";
            }
 
            return result;
        }
}

View without layout in asp.net MVC

If you want to show view without any surrounding _Layout page, you can set Layout=null.

Code sample,

With following custom error view, you displayed without surrounding _Layout page.

@{
    Layout = null;
}
<div class="alert alert-danger alert-dismissible fade show">
    <div>
        <h5>Error! </h5>
        <h6>Something went wrong, contact admin, or try again</h6>
    </div>
</div>