C# Program to Demonstrate DefaultIfEmpty Method


This is a C# Program to demonstrate DefaultIfEmpty case.

Problem Description

This C# Program Demonstrates DefaultIfEmpty case.

Problem Solution

Here the default ifempty case is explained with the student database.

Program/Source Code

Here is source code of the C# Program to Demonstrate DefaultIfEmpty casee. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 *  C# Program to Demonstrate DefaultIfEmpty case
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace program
    class student
        public string Name { get; set; }
        public string stuID { get; set; }
    class studentdetails
        public string SID { get; set; }
        public string City { get; set; }
    class Program
        static void Main(string[] args)
            List<student> objstudent = new List<student>{
                new student{ Name="Bob",stuID="I001"},
                new student{ Name="Vijay",stuID="I002"},
                new student{ Name="Jerry",stuID="I003"},
                new student{ Name="Tom",stuID="I004"},
                new student{ Name="Senthil",stuID="I005"},
            List<studentdetails> objstudentdetails = new List<studentdetails>{
                new studentdetails{ SID="I001",City="Delhi"},
                new studentdetails{ SID="I002",City="Mumbai"},
                new studentdetails{ SID="I007",City="Chennai"},
                new studentdetails{ SID="I008",City="Pune"},
                new studentdetails{ SID="I009",City=""},
            var resultDefaultIfEmpty = from stu in objstudent
                                       join studentdetails in objstudentdetails 
                                       on stu.stuID equals studentdetails.SID 
                                       into ResultEmpstudentdetails
             from output in ResultEmpstudentdetails.DefaultIfEmpty()
             select new
                    studentName = stu.Name,
                    City = output != null ? output.City : null
            Console.WriteLine(string.Join("\n", resultDefaultIfEmpty.Select(stu => 
                        " student Name = " +  stu.studentName + ", City Name = " + 
Program Explanation

This C# program is used to demonstrate DefaultIfEmpty case. Using list functions create the student name, student Id and city. Using DefaultIfEmpty() method return the elements of the specified sequence or the type parameter’s default value in a singleton collection if the sequence is empty from the database. The default ifempty case is explained with the student database.

Runtime Test Cases
 student Name = Bob, City Name = Delhi
 student Name = Vijay, City Name = Mumbai
 student Name = Jerry, City Name =
 student Name = Tom, City Name =
 student Name = Senthil, City Name =

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.