NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io





------------------------------------------------------------------------------------------------------------------------
https://stackoverflow.com/questions/9808035/how-do-i-make-the-return-type-of-a-method-generic


Option 1: Straight approach - Create multiple functions for each type you expect rather than having one generic function.

public static bool ConfigSettingInt(string settingName)
{
return Convert.ToBoolean(ConfigurationManager.AppSettings[settingName]);
}
Option 2: When you don't want to use fancy methods of conversion - Cast the value to object and then to generic type.

public static T ConfigSetting<T>(string settingName)
{
return (T)(object)ConfigurationManager.AppSettings[settingName];
}
Note - This will throw an error if the cast is not valid(your case). I would not recommend doing this if you are not sure about the type casting, rather go for option 3.

Option 3: Generic with type safety - Create a generic function to handle type conversion.

public static T ConvertValue<T,U>(U value) where U : IConvertible
{
return (T)Convert.ChangeType(value, typeof(T));
}


------------------------------------------------------------------------------------------------------------------------
https://json2csharp.com/csharp-object-initializer
public class TestClass
{
public bool TestBool { get; set; }
public Boolean TestBoolean { get; set; }
public string TestString { get; set; }
public String TestString_2 { get; set; }
public Guid TestGuid { get; set; }
public DateTime TestDateTime { get; set; }
public DateTimeOffset TestDateTimeOffset { get; set; }
public char TestChar { get; set; }
}



TestClass testclass = new TestClass() {
TestBool = true,
TestBoolean = true,
TestString = ",
TestString_2 = ",
TestGuid = Guid.NewGuid(),
TestDateTime = DateTime.Now,
TestDateTimeOffset = DateTimeOffset.Now,
TestChar = 'c',
};
------------------------------------------------------------------------------------------------------------------------


------------------------------------------------------------------------------------------------------------------------
https://www.javatpoint.com/csharp-asynchronous-methods
C# Asynchronous Method Example
using System;
using System.Threading.Tasks;
using System.Net.Http;
namespace CSharpFeatures
{
class AsynchronousMethod
{
static void Main(string[] args)
{
Task<int> result = add();
Console.WriteLine("length: {0}", result.Result);
}
// Asynchronous method
async static Task<int> add()
{
Task<string> TaskUrl = new HttpClient().GetStringAsync("http://www.javatpoint.com");
string result = await TaskUrl;
return result.Length;
}
}
}
Output

length: 36006
------------------------------------------------------------------------------------------------------------------------
https://www.dotnetperls.com/generic

C# program that describes generic class
using System;

class Test<T>
{
T _value;

public Test(T t)
{
// The field has the same type as the parameter.
this._value = t;
}

public void Write()
{
Console.WriteLine(this._value);
}
}

class Program
{
static void Main()
{
// Version 1: use int type parameter.
Test<int> test1 = new Test<int>(5);
// Call the Write method.
test1.Write();

// Version 2: use string type parameter.
Test<string> test2 = new Test<string>("cat");
test2.Write();
}
}
5
cat


C# program that declares generic method
using System;
using System.Collections.Generic;

class Program
{
static List<T> GetInitializedList<T>(T value, int count)
{
// This generic method returns a List with ten elements initialized.
// ... It uses a type parameter.
// ... It uses the "open type" T.
List<T> list = new List<T>();
for (int i = 0; i < count; i++)
{
list.Add(value);
}
return list;
}

static void Main()
{
// Use the generic method.
// ... Specifying the type parameter is optional here.
// ... Then print the results.
List<bool> list1 = GetInitializedList(true, 5);
List<string> list2 = GetInitializedList<string>("Perls", 3);
foreach (bool value in list1)
{
Console.WriteLine(value);
}
foreach (string value in list2)
{
Console.WriteLine(value);
}
}
}
True
True
True
True
True
Perls
Perls
Perls



C# program that uses generic type constraints
using System;
using System.Data;

/// <summary>
/// Requires type parameter that implements interface IDisposable.
/// </summary>
class Ruby<T> where T : IDisposable
{
}

/// <summary>
/// Requires type parameter that is a struct.
/// </summary>
class Python<T> where T : struct
{
}

/// <summary>
/// Requires type parameter that is a reference type with a constructor.
/// </summary>
class Perl<V> where V : class, new()
{
}

class Program
{
static void Main()
{
// DataTable implements IDisposable so it can be used with Ruby.
Ruby<DataTable> ruby = new Ruby<DataTable>();

// Int is a struct (ValueType) so it can be used with Python.
Python<int> python = new Python<int>();

// Program is a class with a parameterless constructor (implicit)
// ... so it can be used with Perl.
Perl<Program> perl = new Perl<Program>();
}
}


------------------------------------------------------------------------------------------------------------------------
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.