Skip to content

Commit 2c77190

Browse files
Adding c# snippets
1 parent 3b6cc90 commit 2c77190

File tree

9 files changed

+129
-0
lines changed

9 files changed

+129
-0
lines changed

snippets/csharp/basics/hello-world.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Hello, World!
3+
description: Prints Hello, World! to the terminal.
4+
author: chaitanya-jvnm
5+
tags: c#,printing,hello-world,utility
6+
---
7+
8+
```c#
9+
public class Program {
10+
public static void Main(string[] args) {
11+
System.Console.WriteLine("Hello, World!");
12+
}
13+
}
14+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Hello, World!
3+
description: Generates a new GUID
4+
author: chaitanya-jvnm
5+
tags: c#,guid,generate,utility
6+
---
7+
8+
```c#
9+
public static string GenerateGuid() {
10+
return Guid.NewGuid().ToString();
11+
}
12+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Hello, World!
3+
description: Converts a GUID to a byte array.
4+
author: chaitanya-jvnm
5+
tags: c#,guid,byte-array,utility
6+
---
7+
8+
```c#
9+
public static byte[] GuidToByteArray(string guid) {
10+
return new Guid(guid).ToByteArray();
11+
}
12+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Hello, World!
3+
description: Checks if a string is a valid GUID.
4+
author: chaitanya-jvnm
5+
tags: c#,guid,validate,utility
6+
---
7+
8+
```c#
9+
public static bool IsGuid(string str) {
10+
return Guid.TryParse(str, out _);
11+
}
12+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Hello, World!
3+
description: Decodes a JWT.
4+
author: chaitanya-jvnm
5+
tags: c#,jwt,decode,utility
6+
---
7+
8+
```c#
9+
public static string DecodeJwt(string token) {
10+
return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();
11+
}
12+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Hello, World!
3+
description: Generates a new JWT.
4+
author: chaitanya-jvnm
5+
tags: c#,jwt,generate,utility
6+
---
7+
8+
```c#
9+
public static string GenerateJwt(string secret, string issuer, string audience, int expirationMinutes) {
10+
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret));
11+
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
12+
var token = new JwtSecurityToken(issuer, audience, null, expires: DateTime.UtcNow.AddMinutes(expirationMinutes), signingCredentials: credentials);
13+
return new JwtSecurityTokenHandler().WriteToken(token);
14+
}
15+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Hello, World!
3+
description: Validates a JWT.
4+
author: chaitanya-jvnm
5+
tags: c#,jwt,validate,utility
6+
---
7+
8+
```c#
9+
public static bool ValidateJwt(string token, string secret) {
10+
var tokenHandler = new JwtSecurityTokenHandler();
11+
var validationParameters = new TokenValidationParameters {
12+
ValidateIssuerSigningKey = true,
13+
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),
14+
ValidateIssuer = false,
15+
ValidateAudience = false
16+
};
17+
try {
18+
tokenHandler.ValidateToken(token, validationParameters, out _);
19+
return true;
20+
}
21+
catch {
22+
return false
23+
}
24+
}
25+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Hello, World!
3+
description: Makes the first letter of a string uppercase.
4+
author: chaitanya-jvnm
5+
tags: c#,string,capitalize,utility
6+
---
7+
8+
```c#
9+
/// <summary>
10+
/// Capitalize the first character of the string
11+
/// <summary>
12+
public static string Capitalize(this string str) {
13+
return str.Substring(0, 1).ToUpper() + str.Substring(1);
14+
}
15+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Hello, World!
3+
description: Splits a string by a delimiter.
4+
author: chaitanya-jvnm
5+
tags: c#,string,split,utility
6+
---
7+
8+
```c#
9+
public static string[] SplitString(string str, string delimiter) {
10+
return str.Split(delimiter);
11+
}
12+
```

0 commit comments

Comments
 (0)