TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Generic TryParse in C#.NET
Manoj Bhoir
Apr 18
2015
Code
8
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
public
static
T ? TryParse < T > (
string
value, TryParseHandler < T > handler) where T :
struct
{
if
(String.IsNullOrEmpty(value))
return
null
;
T result;
if
(handler(value,
out
result))
return
result;
Trace.TraceWarning(
"Invalid value '{0}'"
, value);
return
null
;
}
public
delegate
bool
TryParseHandler < T > (
string
value,
out
T result);
//How to use?
var value = TryParse <
int
> (
"123"
,
int
.TryParse);
var value2 = TryParse <
decimal
> (
"123.123"
,
decimal
.TryParse);
C#.NET
.NET
Generic TryParse