How to Convert a String to Int Without Parse in C#

Convert string to int without parse in C#: the string "4815" becomes the int 4815 through a c - '0' digit loop

Interviewers love this one: turn “4815” into an int without calling int.Parse. To convert a string to int without Parse in C#, loop over the characters and build the number yourself: In this tutorial, I’ll write my own convert string to int without parse in C# routine, then add a sign, invalid-character checks and overflow … Read more >>

How to Remove the Last Word from a String in C#

Remove the last word from a string in C#: LastIndexOf finds the last space in Trail Runner Jacket Navy and a range slice prints Trail Runner Jacket, with Navy as the last word

Picture a Portland outdoor store exporting 2,400 product titles for a new catalog. Every title ends with a color, like “Trail Runner Jacket Navy”, and the catalog wants only “Trail Runner Jacket”. That’s a textbook remove last word from string C# job, and the quickest answer is LastIndexOf(' ') plus a range slice up to … Read more >>

C# Constant Naming Conventions: PascalCase, Not ALL_CAPS (.NET 10)

C# constant naming convention: MAX_CART_ITEMS flagged by CA1707 becomes MaxCartItems in PascalCase

Our code reviews still catch MAX_CART_ITEMS in new C# code, usually from developers who came from Java, Python or C. Quick answer: the C# constant naming convention is PascalCase for every const, whether it’s public, private or local, with no underscore prefix: I tested this in a .NET 10 checkout for a Texas outdoor store, … Read more >>

C# Naming Conventions: The Complete Guide with Examples

C# naming conventions cheat sheet: class, interface, method, field, event

A pull request for a Texas retailer’s order system hit my queue last week with custName, CUST_ID, and m_total in one class. That’s three naming conventions fighting inside 40 lines of C#. Here’s the quick answer. C# uses PascalCase for types, namespaces, methods, properties, events, and constants, camelCase for local variables and parameters, _camelCase for … Read more >>

C# Enum Naming Conventions: Rules and Examples (.NET 10)

C# enum naming conventions: ALL_CAPS ORDER_SHIPPED member renamed to PascalCase Shipped in a singular OrderStatus enum

This is the enum I flag most often in code reviews, and it breaks three C# enum naming conventions at once: an Enum suffix, ALL_CAPS members and the type name repeated inside every value. The fix is enum OrderStatus { Pending, Shipped, Delivered }: a singular PascalCase type with plain PascalCase members. Below are the … Read more >>

C# Field Naming Conventions: _camelCase, s_ and PascalCase (.NET 10)

C# field naming convention: name = name; flagged by CS1717 becomes _name = name; with an _camelCase private field

Most field bugs we catch in code review start with name = name; inside a constructor. It compiles with a warning, and the field stays empty, because the parameter hides the field. Quick answer: the C# field naming conventions are _camelCase for private and internal fields, s_ and t_ prefixes for static and thread-static fields, … Read more >>