Can someone please explain what this expression means

jonaohana

New member
Joined
Mar 6, 2022
Messages
1
Programming Experience
10+
public T GetObject<T>() where T : new()
{
return new T();
}

What is T?
What is <T>
What is where
....

Im new to C# dont get any of this
 
This is generics. There is plenty of information on generics at Microsoft. Learn about the where constraint and new constraint.

Using GetObject

C#:
public class Operations
{
    public string Name() => Environment.UserName;
}

public class FileOperations
{
    public string Path => AppDomain.CurrentDomain.BaseDirectory;
}

public class Helpers
{
    public static T GetObject<T>() where T : new() => new T();
}

public class Demos
{
    public static void Run()
    {
        Operations result1 = Helpers.GetObject<Operations>();
        Console.WriteLine(result1.Name());

        FileOperations result2 = Helpers.GetObject<FileOperations>();
        Console.WriteLine(result2.Path);
    }
}

Running above code

C#:
Demos.Run();

Expected results, first line returns your user name while second line returns current path of the app.

So with that, when curious about code as presented here do some research e.g. type into your web browser c# what is T and the first few results will have links to learn from which should then lead your to search at Microsoft docs. When neither of these are enough then is the time to ask in a forum question.
 
Let's look at a useful example for generics, the following method (language extension) provides logic to determine if a value is between two types that implement IComparable interface. In the code below Between is use for int and DateTime. Create a new console project (in this case .NET Core console project) named BetweenCodeSample, replace current code with code below and run. Once ran, read through the code to get an understanding along with reading the docs in my first reply.

C#:
using System;
using System.Collections.Generic;

namespace BetweenCodeSample
{
    /// <summary>
    /// Example for a generic language extension for <see cref="IComparable"/>
    /// In this case the extension is used for int and DateTime
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            
            int age = 29;

            Console.WriteLine($"{age,-3} is over 30 {age.Between(30, 30).ToYesNo()}");

            age = 30;
            Console.WriteLine($"{age,-3} is over 30 {age.Between(30, 30).ToYesNo()}");

            age = 30;
            Console.WriteLine($"{age,-3} is between 19 and 30 {age.Between(19, 30).ToYesNo()}");

            age = 12;
            Console.WriteLine($"is child {age.IsChild().ToYesNo()}");

            DateTime lowDateTime = new (2022, 1, 1);
            DateTime someDateTime = new (2022, 1, 2);
            DateTime highDateTime = new (2022, 1, 8);

            Console.WriteLine($"{someDateTime:d} between {lowDateTime:d} and {highDateTime:d}? {someDateTime.Between(lowDateTime, highDateTime).ToYesNo()}");

            someDateTime = new DateTime(2022, 2, 2);
            Console.WriteLine($"{someDateTime:d} between {lowDateTime:d} and {highDateTime:d}? {someDateTime.Between(lowDateTime, highDateTime).ToYesNo()}");

            Console.Read();
        }
    }
    /// <summary>
    /// Place this class in it's own file
    /// </summary>
    public static class GenericExtensions
    {
        public static bool Between<T>(this T value, T lowerValue, T upperValue)
            where T : struct, IComparable<T>
            => Comparer<T>.Default.Compare(value, lowerValue) >= 0 &&
               Comparer<T>.Default.Compare(value, upperValue) <= 0;

        public static bool IsChild(this int sender)
            => sender.Between(1, 12);

        public static bool IsOver30(this int sender)
            => sender.Between(30, 30);

        public static string ToYesNo(this bool value)
            => value ? "Yes" : "No";

    }
}
 
Hmmm..30.IsOver30() doesn't seem to be right.
 
Hmmm..30.IsOver30() doesn't seem to be right.
Yep, your right.

Should had been

C#:
public static bool IsOver30(this int sender) => sender.IsGreaterThan(30);
public static bool IsGreaterThan<T>(this T sender, T other) where T : IComparable
    => sender.CompareTo(other) > 0;

And to complement IsGreaterThan

C#:
public static bool IsLessThan<T>(this T sender, T other) where T : IComparable
    => sender.CompareTo(other) < 0;
 
Back
Top Bottom