Hi
I have below code & i want it to write only one
Code
using (var yt = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "API Key",
}))Thanks
Hi
I have below code & i want it to write only one
using (var yt = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "API Key",
}))Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pankajkumar PatelRank #181 · 13.4K reputationPosted Dec 15, 2022, 4:53 AM EST
Hi Ramco Ramco,
One of the way is Singleton Design Pattern In C#. Below is one example for reference purpose:
using System; // The 'Singleton' class public class API { static API instance; private string ApiKey; private string ApiKeyTime; // Lock synchronization object private static object locker = new object(); // Constructor (protected) protected API() { ApiKey = "API Key"; ApiKeyTime = Convert.ToString(DateTime.Now.ToLongTimeString());; } public static API GetAPI() { // Support multithreaded applications through // 'Double checked locking' pattern which (once // the instance exists) avoids locking each // time the method is invoked if (instance == null) { lock (locker) { if (instance == null) { instance = new API(); } } } return instance; } public string APIKey { get { return ApiKey; } } public string APIKeyTime { get { return ApiKeyTime; } } } public class Stman { public string[] Name = new string[3]; public static void Main() { API api1 = API.GetAPI(); Console.WriteLine(api1.APIKey + " and " + api1.APIKeyTime); System.Threading.Thread.Sleep(2 * 60 * 1000); API api2 = API.GetAPI(); Console.WriteLine(api2.APIKey + " and " + api2.APIKeyTime); } }For more details:
Hope this will help you!