none
Need help regarding c# code RRS feed

  • Question

  • just see this code i found 

    public class ErrorViewModel
        {
            public string RequestId { get; set; }
    
            public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
        }

    tell me ShowRequestId  is function or property?

    convert this code to old .net version as a result i can understand.

    what term / name can be used to address this kind of function ?

    if my function would be multi line then how can i write function like above one ?

    how this kind of function can have multiple argument ?

    need some insight. thanks

    Friday, February 28, 2020 9:48 AM

Answers

  • Hi,

    ShowRequestId is an Expression Bodied-Property ( https://csharp.christiannagel.com/2017/01/25/expressionbodiedmembers/ )

    You can write the property also like :

     public class ErrorViewModel
        {
            public string RequestId { get; set; }
    
            public bool ShowRequestId
            {
                get { return !string.IsNullOrEmpty(RequestId); }
            }
        }


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me

    Friday, February 28, 2020 10:10 AM
  • Yes you can write functions, constructors , finalizers and indexes like this https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members

    Member Supported as of...
    Method C# 6
    Read-only property C# 6
    Property C# 7.0
    Constructor C# 7.0
    Finalizer C# 7.0
    Indexer C# 7.0


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me


    • Edited by KeesDijk Friday, February 28, 2020 1:17 PM
    • Marked as answer by Tridip Sunday, March 1, 2020 6:45 PM
    Friday, February 28, 2020 1:17 PM
  • See the following

    https://riptutorial.com/csharp/example/44/expression-bodied-function-members

    Expression-bodied function members have some limitations. They can't contain block statements and any other statements that contain blocks: if, switch, for, foreach, while, do, try, etc.

    Some if statements can be replaced with ternary operators. Some for and foreach statements can be converted to LINQ queries, for example:

    IEnumerable<string> Digits
    {
        get
        {
            for (int i = 0; i < 10; i++)
                yield return i.ToString();
        }
    }

    In all other cases, the old syntax for function members can be used.

    Expression-bodied function members can contain async/await, but it's often redundant:

    async Task<int> Foo() => await Bar();  

    Can be replaced with:

    Task<int> Foo() => Bar();

    In regards to functions there are also inline functions, not the best example but gets the point across.

    using System;
    using System.Configuration;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string GetConnectionString()
                {
                    return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                }
                Console.WriteLine(GetConnectionString());
                Console.ReadLine();
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Tridip Sunday, March 1, 2020 6:46 PM
    Friday, February 28, 2020 1:18 PM
    Moderator

All replies

  • Hi,

    ShowRequestId is an Expression Bodied-Property ( https://csharp.christiannagel.com/2017/01/25/expressionbodiedmembers/ )

    You can write the property also like :

     public class ErrorViewModel
        {
            public string RequestId { get; set; }
    
            public bool ShowRequestId
            {
                get { return !string.IsNullOrEmpty(RequestId); }
            }
        }


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me

    Friday, February 28, 2020 10:10 AM
  • this is new way of writing property. from which version C# support this one ?

    any new way introduced to write function like above one?

    thanks

    Friday, February 28, 2020 12:18 PM
  • Yes you can write functions, constructors , finalizers and indexes like this https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members

    Member Supported as of...
    Method C# 6
    Read-only property C# 6
    Property C# 7.0
    Constructor C# 7.0
    Finalizer C# 7.0
    Indexer C# 7.0


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me


    • Edited by KeesDijk Friday, February 28, 2020 1:17 PM
    • Marked as answer by Tridip Sunday, March 1, 2020 6:45 PM
    Friday, February 28, 2020 1:17 PM
  • See the following

    https://riptutorial.com/csharp/example/44/expression-bodied-function-members

    Expression-bodied function members have some limitations. They can't contain block statements and any other statements that contain blocks: if, switch, for, foreach, while, do, try, etc.

    Some if statements can be replaced with ternary operators. Some for and foreach statements can be converted to LINQ queries, for example:

    IEnumerable<string> Digits
    {
        get
        {
            for (int i = 0; i < 10; i++)
                yield return i.ToString();
        }
    }

    In all other cases, the old syntax for function members can be used.

    Expression-bodied function members can contain async/await, but it's often redundant:

    async Task<int> Foo() => await Bar();  

    Can be replaced with:

    Task<int> Foo() => Bar();

    In regards to functions there are also inline functions, not the best example but gets the point across.

    using System;
    using System.Configuration;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string GetConnectionString()
                {
                    return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                }
                Console.WriteLine(GetConnectionString());
                Console.ReadLine();
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Tridip Sunday, March 1, 2020 6:46 PM
    Friday, February 28, 2020 1:18 PM
    Moderator
  • can you plzz convert your below function to old style so i can understand the usage

    async Task<int> Foo() => await Bar();  
    Task<int> Foo() => Bar();

    thanks

    Sunday, March 1, 2020 6:47 PM