Skip to content

feat: Adding CSharp Snippets #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions public/consolidated/csharp.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,110 @@
[
{
"categoryName": "Basics",
"snippets": [
{
"title": "Hello, World!",
"description": "Prints Hello, World! to the terminal.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"printing",
"hello-world",
"utility"
],
"contributors": [],
"code": "public class Program {\n public static void Main(string[] args) {\n System.Console.WriteLine(\"Hello, World!\");\n }\n}\n"
}
]
},
{
"categoryName": "Guid Utilities",
"snippets": [
{
"title": "Hello, World!",
"description": "Generates a new GUID",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"guid",
"generate",
"utility"
],
"contributors": [],
"code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n"
},
{
"title": "Hello, World!",
"description": "Converts a GUID to a byte array.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"guid",
"byte-array",
"utility"
],
"contributors": [],
"code": "public static byte[] GuidToByteArray(string guid) {\n return new Guid(guid).ToByteArray();\n}\n"
},
{
"title": "Hello, World!",
"description": "Checks if a string is a valid GUID.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"guid",
"validate",
"utility"
],
"contributors": [],
"code": "public static bool IsGuid(string str) {\n return Guid.TryParse(str, out _);\n}\n"
}
]
},
{
"categoryName": "Jwt Utilities",
"snippets": [
{
"title": "Hello, World!",
"description": "Decodes a JWT.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"jwt",
"decode",
"utility"
],
"contributors": [],
"code": "public static string DecodeJwt(string token) {\n return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();\n}\n"
},
{
"title": "Hello, World!",
"description": "Generates a new JWT.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"jwt",
"generate",
"utility"
],
"contributors": [],
"code": "public static string GenerateJwt(string secret, string issuer, string audience, int expirationMinutes) {\n var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret));\n var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);\n var token = new JwtSecurityToken(issuer, audience, null, expires: DateTime.UtcNow.AddMinutes(expirationMinutes), signingCredentials: credentials);\n return new JwtSecurityTokenHandler().WriteToken(token);\n}\n"
},
{
"title": "Hello, World!",
"description": "Validates a JWT.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"jwt",
"validate",
"utility"
],
"contributors": [],
"code": "public static bool ValidateJwt(string token, string secret) {\n var tokenHandler = new JwtSecurityTokenHandler();\n var validationParameters = new TokenValidationParameters {\n ValidateIssuerSigningKey = true,\n IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),\n ValidateIssuer = false,\n ValidateAudience = false\n };\n try {\n tokenHandler.ValidateToken(token, validationParameters, out _);\n return true;\n }\n catch {\n return false\n }\n}\n"
}
]
},
{
"categoryName": "List Utilities",
"snippets": [
Expand All @@ -20,6 +126,32 @@
{
"categoryName": "String Utilities",
"snippets": [
{
"title": "Hello, World!",
"description": "Makes the first letter of a string uppercase.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"string",
"capitalize",
"utility"
],
"contributors": [],
"code": "/// <summary>\n/// Capitalize the first character of the string\n/// <summary>\npublic static string Capitalize(this string str) {\n return str.Substring(0, 1).ToUpper() + str.Substring(1);\n}\n"
},
{
"title": "Hello, World!",
"description": "Splits a string by a delimiter.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"string",
"split",
"utility"
],
"contributors": [],
"code": "public static string[] SplitString(string str, string delimiter) {\n return str.Split(delimiter);\n}\n"
},
{
"title": "Truncate a String",
"description": "Cut off a string once it reaches a determined amount of characters and add '...' to the end of the string",
Expand Down
10 changes: 10 additions & 0 deletions public/icons/csharp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions snippets/csharp/basics/hello-world.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: Hello, World!
description: Prints Hello, World! to the terminal.
author: chaitanya-jvnm
tags: c#,printing,hello-world,utility
---

```c#
public class Program {
public static void Main(string[] args) {
System.Console.WriteLine("Hello, World!");
}
}
```
12 changes: 12 additions & 0 deletions snippets/csharp/guid-utilities/generate-guid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Hello, World!
description: Generates a new GUID
author: chaitanya-jvnm
tags: c#,guid,generate,utility
---

```c#
public static string GenerateGuid() {
return Guid.NewGuid().ToString();
}
```
12 changes: 12 additions & 0 deletions snippets/csharp/guid-utilities/guid-to-byte-array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Hello, World!
description: Converts a GUID to a byte array.
author: chaitanya-jvnm
tags: c#,guid,byte-array,utility
---

```c#
public static byte[] GuidToByteArray(string guid) {
return new Guid(guid).ToByteArray();
}
```
12 changes: 12 additions & 0 deletions snippets/csharp/guid-utilities/validate-guid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Hello, World!
description: Checks if a string is a valid GUID.
author: chaitanya-jvnm
tags: c#,guid,validate,utility
---

```c#
public static bool IsGuid(string str) {
return Guid.TryParse(str, out _);
}
```
12 changes: 12 additions & 0 deletions snippets/csharp/jwt-utilities/decode-jwt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Hello, World!
description: Decodes a JWT.
author: chaitanya-jvnm
tags: c#,jwt,decode,utility
---

```c#
public static string DecodeJwt(string token) {
return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();
}
```
15 changes: 15 additions & 0 deletions snippets/csharp/jwt-utilities/generate-jwt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Hello, World!
description: Generates a new JWT.
author: chaitanya-jvnm
tags: c#,jwt,generate,utility
---

```c#
public static string GenerateJwt(string secret, string issuer, string audience, int expirationMinutes) {
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(issuer, audience, null, expires: DateTime.UtcNow.AddMinutes(expirationMinutes), signingCredentials: credentials);
return new JwtSecurityTokenHandler().WriteToken(token);
}
```
25 changes: 25 additions & 0 deletions snippets/csharp/jwt-utilities/validate-jwt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Hello, World!
description: Validates a JWT.
author: chaitanya-jvnm
tags: c#,jwt,validate,utility
---

```c#
public static bool ValidateJwt(string token, string secret) {
var tokenHandler = new JwtSecurityTokenHandler();
var validationParameters = new TokenValidationParameters {
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),
ValidateIssuer = false,
ValidateAudience = false
};
try {
tokenHandler.ValidateToken(token, validationParameters, out _);
return true;
}
catch {
return false
}
}
```
15 changes: 15 additions & 0 deletions snippets/csharp/string-utilities/capitalize-first-letter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Hello, World!
description: Makes the first letter of a string uppercase.
author: chaitanya-jvnm
tags: c#,string,capitalize,utility
---

```c#
/// <summary>
/// Capitalize the first character of the string
/// <summary>
public static string Capitalize(this string str) {
return str.Substring(0, 1).ToUpper() + str.Substring(1);
}
```
12 changes: 12 additions & 0 deletions snippets/csharp/string-utilities/split-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Hello, World!
description: Splits a string by a delimiter.
author: chaitanya-jvnm
tags: c#,string,split,utility
---

```c#
public static string[] SplitString(string str, string delimiter) {
return str.Split(delimiter);
}
```