class that can only be instantiated with the using keyword

Berkay Akar 26 Reputation points
2022-10-21T13:29:36.927+00:00

Hello dear users

I'm new to c# and .net. I have a problem. I want to create a class. this class have a option.

When you want create instance my class you must to use the "using" keyword. it will force you to use the keyword.

![252970-image.png]2

some class example

usage example scenario

![252979-image.png]3

if you want to create instance you must to using keyword with create instance

it didnt work create instance witout use using keyword.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,575 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
326 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,581 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lex Li (Microsoft) 5,157 Reputation points Microsoft Employee
    2022-10-22T03:22:27.21+00:00

    That's simply impossible in the current C# syntax (no matter how eager you want it), as even for built-in classes such as SqlConnection you can feel free to create new instances without using.

    The philosophy since C# 1.0 in 2000 you should adapt to is that instead of forcing the users to remember using, you should allow them to forget that. To avoid memory leak, follow the standard IDisposable pattern to properly implement the Dispose method and finalizer.

    If this is your time to hear the concept of finalization, you need to read more from key articles like

    https://video2.skills-academy.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern


2 additional answers

Sort by: Most helpful
  1. AgaveJoe 27,496 Reputation points
    2022-10-21T13:42:59.81+00:00

    This is the 3rd time you've posted the same question. Please stop posting the same question and make an effort to read your other posts for the solution!

    You are missing the using statement at the top of the code file.

    For example

    using System;  
    using InstanceExample;  
      
    namespace ConsoleApp1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                Person p = new Person();  
            }  
        }  
    }  
      
    namespace InstanceExample  
    {  
        public class Person  
        {  
            public string Name { get; set; }  
        }  
    }  
      
    

    Or use the fully qualified name.

    using System;  
      
    namespace ConsoleApp1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                InstanceExample.Person p = new InstanceExample.Person();  
            }  
        }  
    }  
      
    namespace InstanceExample  
    {  
        public class Person  
        {  
            public string Name { get; set; }  
        }  
    }  
      
    

    Please read the namespace documentation which was also included in your other posts!

    Declare namespaces to organize types

    If the class is in a class library then along with the using statement a class reference is required.

    How to: Add or remove references by using the Reference Manager
    Find references in your code


  2. AgaveJoe 27,496 Reputation points
    2022-10-21T14:08:54.837+00:00

    if i want to create instance this class the using keyword must this operation. because this class inherited IDisposible interface.

    Your question is not clear and the source code does not inherit from IDisposable. The type must inherit from IDisposible for use in a using statement. The compiler message has a link to the reference documentation.

    Compiler Error CS1674

    253046-capture.png

    but this class can be instance without using the "using" keyword

    Correct! The "using" statement is not required to instantiate a class. The code can dispose the object.

    using statement (C# Reference)

    using System;  
    using InstanceExample;  
      
    namespace ConsoleApp1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                Person person = new Person() { Name = "Hello World!" };  
                  
                using (person)  
                {  
                    Console.WriteLine(person.Name);  
                }  
      
                Console.WriteLine(person.Name ?? "No name here!");  
      
            }  
        }  
    }  
      
    namespace InstanceExample  
    {  
        public class Person : IDisposable  
        {  
            public string Name { get; set; }  
      
            public void Dispose()  
            {  
                Name = null;  
            }  
        }  
      
        public class AnotherPerson  
        {  
      
        }  
    }