
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How can we return null from a generic method in C#?
Generics allows us to define a class with placeholders for the type of its fields, methods, parameters, etc. Generics replace these placeholders with some specific type at compile time. A generic can be defined using angle brackets <>. A primary limitation of collections is the absence of effective type checking. This means that you can put any object in a collection because all classes in the C# programming language extend from the object base class.
Also, we cannot simply return null from a generic method like in normal method. Below is the error that a generic method will throw if we are trying to return null.
using System; namespace DemoApplication { class Program { public static void Main() { Add(5, 5); } public static T Add<T>(T parameter1, T parameter2) { return null; } } }
So, to return a null or default value from a generic method we can make use default(). default(T) will return the default object of the type which is provided.
Example
using System; namespace DemoApplication { class Program { public static void Main() { Add(5, 5); Console.ReadLine(); } public static T Add<T>(T parameter1, T parameter2) { var defaultVal = default(T); Console.WriteLine(defaultVal); return defaultVal; } } }
Output
The output of the code is
0
Here we could see that the default value of integer 0 is returned.
- Related Articles
- Can we return this keyword from a method in java?
- How can we return multiple values from a function in C/C++?
- How can we return multiple values from a function in C#?
- How can we return a dictionary from a Python function?
- Can we have generic constructors in Java?
- How can I return 0 for NULL in MySQL?
- Return a C# tuple from a method
- How can we check for NULL in a MySQL query?
- Can we change return type of main() method in java?
- Can we throw an object of generic class in java?
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- Can we have multiple type parameters in generic methods in Java?
- Defining generic method inside Non-generic class in Java
- Can we insert null values in a Java list?
- Can we call a constructor directly from a method in java?
