© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•11mo ago•
5 replies
Faker

✅ Is it possible to override ToString method for Anonymous type in C#? (Not using overriden one)

Hello guys, just wondering if it was possible to use another implementation of the
ToString
ToString
method for anonymous type.

I was just using LINQ to perform the following:

var lessons = context.Lessons.Select(lessons => new
            {
                StudentFirstName = lessons.Student.FirstName,
                StudentLastName = lessons.Student.LastName,
                StudentEmail = lessons.Student.Email,
                InstructorFirstName = lessons.Instructor.FirstName,
                InstructorLastName = lessons.Instructor.LastName,
                InstructorEmail = lessons.Instructor.Email,
                CarTransmission = lessons.Car.Transmission,
                LessonDate = lessons.Date
            });
var lessons = context.Lessons.Select(lessons => new
            {
                StudentFirstName = lessons.Student.FirstName,
                StudentLastName = lessons.Student.LastName,
                StudentEmail = lessons.Student.Email,
                InstructorFirstName = lessons.Instructor.FirstName,
                InstructorLastName = lessons.Instructor.LastName,
                InstructorEmail = lessons.Instructor.Email,
                CarTransmission = lessons.Car.Transmission,
                LessonDate = lessons.Date
            });

When I use a foreach and output each lesson, I have the overriden format of the ToString method.

I manage to change part of it using the following:

Console.WriteLine(lesson.ToString().Replace("{","").Replace("}","").TrimStart());
Console.WriteLine(lesson.ToString().Replace("{","").Replace("}","").TrimStart());

But just wanted to know if it's actually possible to change the ToString format again
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Is it possible to use union type in C#?
C#CC# / help
3y ago
❔ Is it possible to scan for viruses/abnormal text in .txt using c#?
C#CC# / help
4y ago
✅ Anonymous type
C#CC# / help
3y ago
is it possible to open an image file using c#?
C#CC# / help
2y ago