-
-
Notifications
You must be signed in to change notification settings - Fork 130
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
Mathys-Gasnier
merged 6 commits into
quicksnip-dev:main
from
chaitanya-jvnm:create-csharp-snippets
Jan 2, 2025
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3b6cc90
Update consolidated snippets
actions-user 2c77190
Adding c# snippets
chaitanya-jvnm 94962ab
Update consolidated snippets
actions-user 59eb516
Added Examples and Removed straightforward snippets
chaitanya-jvnm eb1ab7f
Merge branch 'create-csharp-snippets' of https://github.com/chaitanya…
chaitanya-jvnm 55bd1e5
Update consolidated snippets
actions-user File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 _); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
snippets/csharp/string-utilities/capitalize-first-letter.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.