TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Expression Bodied Constructors and Finalizers

C# Expression Bodied Constructors and Finalizers for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

C# Expression bodied Constructors and Finalizers

C# expression body is a single line expression statement. It is used to provide single life definition to the method, constructor or property.

C# expression bodied constructor is a constructor that contains single line expression statement. The body of constructor does not contain anything except a single expression statement.

It is a concise way to perform some single line operations. Let's see an example.

C# Expression Constructors Example

using System;
namespace CSharpFeatures
{
    class Student
    {
        public string Name { get; set; }
        // Expression Constructor
        public Student(string name) => Name = name;
    }
    class ExpressionConstructor
    {
        public static void Main()
        {
            Student student = new Student("Rahul");
            Console.WriteLine($"Hello {student.Name}");
        }
    }
}	

Output:

Hello Rahul

C# Expression Bodied Finalizer

Finalizer is a destroyer that is used to perform cleanup related tasks. Body definition of finalizer is a single line expression.

While working with finalizer, following are the key points to remember.

  • It can destruct only class instance
  • A class can have only one finalizer
  • It can't be overloaded or inherited
  • It invokes automatically
  • It does not contain parameters

C# Expression Bodied Finalizer Example

using System;
namespace CSharpFeatures
{
    class Student
    {
        public string Name { get; set; }
        // Expression bodied constructor
        public Student(string name) => Name = name;
        // Expression bodied finalizer
        ~Student() => Console.WriteLine("Finalizer Executing");
    }
    class ExpressionConstructor
    {
        public static void Main()
        {
            Student student = new Student("Rahul");
            Console.WriteLine($"Hello {student.Name}");
        }
    }
}

Output:

Hello Rahul
Finalizer Executing





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf