superche
superche

Reputation: 773

What's the C# equivalent of Java CharSequence type?

I'm working on converting a Java project to C#, but don't know what CharSequence is corresponding to C# type.

Upvotes: 2

Views: 1893

Answers (4)

tkefauver
tkefauver

Reputation: 518

A Java.Lang.String is an ICharSequence in C#.

This extension would convert a string to an ICharSequence:

public static ICharSequence ToCharSequence(this string text) {
    return text == null ? null : new Java.Lang.String(text);
}

Upvotes: 1

Gauravsa
Gauravsa

Reputation: 6514

A CharSequence is a sequence of characters which is implemented by StringBuilder, StringBuffer, String.

The closest to CharSequence in C# is:

char[] charArr = sentence.ToCharArray();

Strings are immutable in Java and C#. There is no mutable CharSequence in C#.

A CharSequence is basically a bridge between String and StringBuilder in Java.

C# being the best language doesnt suffer from identity crisis.

You are better off using String or StringBuilder in C#.

Upvotes: 0

Andreas
Andreas

Reputation: 159086

There is no direct equivalent in c#. Use string instead.

The Java CharSequence is a simple abstraction, that allows methods with simple needs to accept a String, CharBuffer, Segment, StringBuffer, or StringBuilder without having to convert the others to String first.

It allows minor performance improvement in Java for some API methods.

Upvotes: 5

Django
Django

Reputation: 222

CharSequence is an interface that represents a sequence of characters. To hold char array, string (immutable) or StringBuilder classes can be used in c#.

Upvotes: 0

Related Questions